This example describes a JavaScript implementation that complements the 0 output of a number in a specified length. Share to everyone for your reference. The specific analysis is as follows:
For example, we want to output the number of the length is fixed, assuming that 10, if the number is 123, then output 0000000123, not enough digits in the previous replenishment of 0, here provide three different ways to achieve the number of JS code to 0 of the operation
Method 1
function Prefixinteger (num, length) {return
(Num/math.pow (10,length)). toFixed (length). substr (2);
}
Method 2, more efficient
function Prefixinteger (num, length) {return
("0000000000000000" + num). substr (-length);
and more efficient.
function Prefixinteger (num, length) {return
(Array (length). Join (' 0 ') + num). Slice (-length);
}
I hope this article will help you with your JavaScript programming.