Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 这个将数字转换成另一个数字的脚本好吗?_Javascript - Fatal编程技术网

Javascript 这个将数字转换成另一个数字的脚本好吗?

Javascript 这个将数字转换成另一个数字的脚本好吗?,javascript,Javascript,诺布在这里。我不知道为什么在这里问。对不起我的英语。脚本的目的是将类似于1,34664的数字转换为类似于3位的另一位数字:001034664。 以下是代码: //Get number let num = 45 //Convert it let counter = num.toString().padStart(4, "0") //Result of thi is 0045 另一个代码: var i =1 var count = i + ''; while (count.length <

诺布在这里。我不知道为什么在这里问。对不起我的英语。脚本的目的是将类似于1,34664的数字转换为类似于3位的另一位数字:001034664。 以下是代码:

//Get number
let num = 45
//Convert it
let counter = num.toString().padStart(4, "0")
//Result of thi is 0045

另一个代码:

var i =1
var count = i + '';
while (count.length < 3) {
  count = '0' + count;
}   
第二个自动获取值,但这个脚本的功能是相同的,我想是这样的。 哪一个最好?
有更好的办法吗?

他们都很好。一般来说,不要重新发明轮子,所以第一个更好。

num.toString10.padStart4,0…这是什么意思?如果使用toString,请不要错过指示要使用的基的基的含义?如果源字符串上有逗号,则可以将其拆分,并用零填充第一个元素。这个答案最好在注释中给出