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/