javascript - 如何将带有序数后缀的数字转换为javascript中的数字

标签 javascript node.js

let string = "13th"

string = Number(string)

我希望它返回 13,而不是我得到 NaN,请问我该如何处理

最佳答案

尝试使用parseInt()

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.

let string = "13th";

string = parseInt(string);
console.log(string);

关于javascript - 如何将带有序数后缀的数字转换为javascript中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60377579/

相关文章:

javascript - api调用Nodejs之间的延迟

node.js - 套接字发送到房间适用于除发送者之外的所有套接字

javascript - express.js 样板代码的用途是什么?

javascript - 使用javascript获取带有复选标记字符的字符串片段

javascript - 从 $watch 触发 ngModelController $parsers 管道

javascript - Node 模块外的返回值

javascript - 单击两次时 .focus() 不聚焦

node.js - 如何创建特定于用户的实体?

javascript - 我需要制作一个插件,该插件将让用户选择生效

android - 如何让npm脚本构建正确的环境?