如何用前导零填充值？

2020/09/28 11:01 · javascript ·  · 0评论

``````var n=-0.1;
n.toLocaleString('en', {minimumIntegerDigits:4,minimumFractionDigits:2,useGrouping:false})
``````

...将输出“ -0000.10”。

``````// or
``````

...将输出“ -0000.1”。

``````function zeroFill( number, width )
{
width -= number.toString().length;
if ( width > 0 )
{
return new Array( width + (/\./.test( number ) ? 2 : 1) ).join( '0' ) + number;
}
return number + ""; // always return a string
}
``````

``````var pad = "000000";
var n = '5';
``````

``````function paddy(num, padlen, padchar) {
}
var fu = paddy(14, 5); // 00014
var bar = paddy(2, 4, '#'); // ###2
``````

``````var zerofilled = ('0000'+n).slice(-4);
``````

``````function zeroPad(num, numZeros) {
var n = Math.abs(num);
var zeros = Math.max(0, numZeros - Math.floor(n).toString().length );
var zeroString = Math.pow(10,zeros).toString().substr(1);
if( num < 0 ) {
zeroString = '-' + zeroString;
}

return zeroString+n;
}
``````

``````> zeroPad(50,4);
"0050"
``````

``````> zeroPad(51234, 3);
"51234"
``````

``````> zeroPad(51.1234, 4);
"0051.1234"
``````

``````Number.prototype.leftZeroPad = function(numZeros) {
var n = Math.abs(this);
var zeros = Math.max(0, numZeros - Math.floor(n).toString().length );
var zeroString = Math.pow(10,zeros).toString().substr(1);
if( this < 0 ) {
zeroString = '-' + zeroString;
}

return zeroString+n;
}
``````

``````Number.prototype.leftZeroPad = function(numZeros) {
var n = Math.abs(this);
var zeros = Math.max(0, numZeros - n.toString().length );
var zeroString = Math.pow(10,zeros).toString().substr(1);
if( this < 0 ) {
zeroString = '-' + zeroString;
}

return zeroString+n;
}
``````

XDR提出了对数变化形式，似乎表现更好。

``````function zeroPad (num, numZeros) {
var an = Math.abs (num);
var digitCount = 1 + Math.floor (Math.log (an) / Math.LN10);
if (digitCount >= numZeros) {
return num;
}
var zeroString = Math.pow (10, numZeros - digitCount).toString ().substr (1);
return num < 0 ? '-' + zeroString + an : zeroString + an;
}
``````

``````("0000000" + number).slice(-7)
``````

``````("0".repeat(padding) + number).slice(-padding)
``````

``````number.toString().padStart(padding, "0")
``````

``````string.padStart(maxLength, "0");
``````

``````string = "14";
maxLength = 5; // maxLength is the max string length, not max # of fills
console.log(res); // prints "00014"``````

``````var amount = 5;

var text = amount.toLocaleString('en-US',
{
style: 'decimal',
minimumIntegerDigits: 3,
useGrouping: false
});
``````

``````var amount = 5;

var text = amount.toLocaleString('en-US',
{
style: 'decimal',
minimumFractionDigits: 2,
useGrouping: false
});
``````

``````var fillZeroes = "00000000000000000000";  // max number of zero fill ever asked for in global

function zeroFill(number, width) {
// make sure it's a string
var input = number + "";
var prefix = "";
if (input.charAt(0) === '-') {
prefix = "-";
input = input.slice(1);
--width;
}
var fillAmt = Math.max(width - input.length, 0);
return prefix + fillZeroes.slice(0, fillAmt) + input;
}
``````

``````y = (new Array(count + 1 - x.toString().length)).join('0') + x;
``````

``````function zerofill(number, length) {
// Setup
var result = number.toString();
var pad = length - result.length;

result = '0' + result;
}

return result;
}
``````

``````(offset + n + '').substr(1);
``````

``````(1e5 + 123 + '').substr(1); // => 00123
``````

``````(0x100000 + 0x123).toString(16).substr(1); // => 00123
``````

``````/** Pad a number with 0 on the left */
var num = number+"";
while(num.length < digits){
num='0'+num;
}
return num;
}
``````

``````Number.prototype.zeroPad=function(digits){
var num=this+"";
while(num.length < digits){
num='0'+num;
}
return(num);
};
``````

``````(value+100000).toString().slice(-5) // "00123" with value=123
``````

``````'abc'.padStart(10);         // "       abc"
``````

``````numberStr.padStart(numberLength, "0");
``````

``````function zeroFill(num, numLength) {
var numberStr = num.toString();

}

var numbers = [0, 1, 12, 123, 1234, 12345];

numbers.forEach(
function(num) {
var numString = num.toString();

}
);``````

x =要填充的整数

y =要填充的零个数

``````function zeroPad(x, y)
{
y = Math.max(y-1,0);
var n = (x / Math.pow(10,y)).toFixed(y);
return n.replace('.','');
}
``````

OP的问题的一个线性解决方案，即数字5的6位零填充表示为：

``console.log(("00000000" + 5).substr(-6));``

``````function pad(num, len) { return ("00000000" + num).substr(-len) };

//gist.github.com/NextToNothing/6325915

``````function pad(str, max, padder) {
}
``````

``````pad(1, 3); // Returns '001'
// - Or -
pad(1, 3, "x"); // Returns 'xx1'
``````

``````function padLeft(str, len, pad) {
str = str + "";
while(str.length < len) {
}
return str;
}

// Usage
// - Or -
padLeft(1, 3, "x"); // Returns 'xx1'
``````

``````Number.prototype.padLeft = function(len, pad) {
var str = this + "";
while(str.length < len) {
}
return str;
}

// Usage
var num = 1;

// - Or -
``````

``````function zPad(n, l, r){
return(a=String(n).match(/(^-?)(\d*)\.?(\d*)/))?a[1]+(Array(l).join(0)+a[2]).slice(-Math.max(l,a[2].length))+('undefined'!==typeof r?(0<r?'.':'')+(a[3]+Array(r+1).join(0)).slice(0,r):a[3]?'.'+a[3]:''):0
}
``````

``````           zPad(6, 2) === '06'
``````

jsFiddle

``````var numToPad = '5';

``````

``````function pad(num, len) {
return '0'.repeat(len - num.toString().length) + num;
}

`_.padLeft(number, 6, '0')`

``````var number = 2
number.toLocaleString(undefined, {minimumIntegerDigits:2})
``````

`output: "02"`

``````function zeroFill(text, size)
{
while (text.length < size){
text = "0" + text;
}

return text;
}``````

``````zeroPad = function (num, count) {
var pad = (num + '').length - count;
num = '0' + num;
}
return num;
};
``````

``````Number.prototype.PadLeft = function (length, digit) {
var str = '' + this;
while (str.length < length) {
str = (digit || '0') + str;
}
return str;
};
``````

``````var a = 567.25;

var b = 567.25;
``````

``````function padZero(s, n) {
s = s.toString(); // in case someone passes a number
return s.length >= n ? s : padZero('0' + s, n);
}
``````

``````String.prototype.padLeft = function (n, c) {
if (isNaN(n))
return null;
c = c || "0";
return (new Array(n).join(c).substring(0, this.length-n)) + this;
};
``````
``````function pad(toPad, padChar, length){
}
``````

`pad(5, 0, 6)` = `000005`

`pad('10', 0, 2)` = `10 // don't pad if not necessary`

`pad('S', 'O', 2)` = `SO`

...等等。

``````function zerofill(number,length) {
var output = number.toString();
while(output.length < length) {
output = '0' + output;
}
return output;
}
``````