This article describes the method of implementing JavaScript to make up for zero output as specified length as a number. Share it for your reference. The specific analysis is as follows:
For example, we hope that the output number length is fixed, assuming it is 10. If the number is 123, then output 0000000123. If there are not enough digits, make up 0 before. Here are three different ways to implement the operation of JS code to make up 0 for the number
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 );}There are more efficient ones
function PrefixInteger(num, length) { return (Array(length).join('0') + num).slice(-length);}I hope this article will be helpful to everyone's JavaScript programming.