如何将带有后缀的数字转换为javascript中的数字 [英] How do I convert number with ordinal suffix to Number in javascript

查看:53
本文介绍了如何将带有后缀的数字转换为javascript中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

let string = "13th"

string = Number(string)

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

I expect it to return 13 instead I get NaN please how do I go about it

推荐答案

尝试使用 parseInt()

如果parseInt遇到指定基数中不是数字的字符,它将忽略该字符和所有后续字符,并返回到该点为止已解析的整数值. 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中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆