在jQuery/JS中的数字之前添加0 [英] adding 0's before a number in jquery/ js
本文介绍了在jQuery/JS中的数字之前添加0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个需要6个数字的柜台
Hi I have a counter which requires 6 numbers
所以可能是
000456 001250 015554
000456 001250 015554
等
但是问题是我得到的号码是456或1250,然后我需要将其转换为000456或001250.
but the problem is i get the number as 456 or 1250 and I then need to convert this to 000456 or 001250.
我将如何去做?
谢谢
丹
推荐答案
对于前导零,您需要显示为字符串,因此,假设您知道自己的数字永远不会超过六位数,则可以执行以下操作:
For leading zeros you need to display as a string, so assuming you know that your numbers will never exceed six digits you can do this:
var num = // your number
var output = ("000000" + num).slice(-6);
要允许更大的数字:
var output = num > 99999 ? num : ("000000" + num).slice(-6);
这篇关于在jQuery/JS中的数字之前添加0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文