本文介绍了在Javascript中删除数字中的前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是最简单和跨浏览器兼容的方法,可以从Javascript中的数字中删除前导零?
例如如果我将文本框的值设置为014或065,则它应该只返回14或65 试试。
parseInt(065,10);
第二个参数用于,并根据文档应始终指定。 MDN表示,
What is the simplest and cross-browser compatible way to remove leading zeros from a number in Javascript ?
e.g. If I have a textbox value as 014 or 065, it should only return 14 or 65
解决方案
Try parseInt.
parseInt("065", 10);
The second parameter is for the radix and as per the documentation it should always be specified. MDN says,
For example, in some implementations parseInt("01803")
will yield 1
. Assuming the goal is to get 1803
, call parseInt with a radix of 10 to ensure the parsing is done in a base decimal numbering system. That is: parseInt("01803", 10)
will yield 1803
. Please note that for a non-integer value, parseInt() will round the number to return as an integer. paseInt("018.03", 10)
will yield 18
.
这篇关于在Javascript中删除数字中的前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!