5

In C# whenever I wanted to print two digit numbers I've used

int digit=1;
Console.Write(digit.ToString("00"));

How can I do the same action in Javascript ?
Thanks

Mohammad Dayyan
  • 21,578
  • 41
  • 164
  • 232
  • possible duplicate of [Javascript printf/string.format](http://stackoverflow.com/questions/610406/javascript-printf-string-format) – Jerod Venema Jun 02 '11 at 21:43

4 Answers4

14

c# digit.toString("00") appends one zero to the left of digit (left padding). In javascript I use this functon for that:

function zeroPad(nr,base){
  var  len = (String(base).length - String(nr).length)+1;
  return len > 0? new Array(len).join('0')+nr : nr;
}
zeroPad(1,10);   //=> 01
zeroPad(1,100);  //=> 001
zeroPad(1,1000); //=> 0001

You can also rewrite it as an extention to Number:

Number.prototype.zeroPad = Number.prototype.zeroPad || 
     function(base){
       var nr = this, len = (String(base).length - String(nr).length)+1;
       return len > 0? new Array(len).join('0')+nr : nr;
    };
 //usage:
(1).zeroPad(10);   //=> 01
(1).zeroPad(100);  //=> 001
(1).zeroPad(1000); //=> 0001

[edit oct. 2021]

A static es20xx Number method, also suitable for negative numbers.

Number.padLeft = (nr, len = 2, padChr = `0`) => 
  `${nr < 0 ? `-` : ``}${`${Math.abs(nr)}`.padStart(len, padChr)}`;
console.log(Number.padLeft(3));
console.log(Number.padLeft(284, 5));
console.log(Number.padLeft(-32, 12));
console.log(Number.padLeft(-0)); // Note: -0 is not < 0
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • It should be noted that this won't work with negative numbers, i.e. it's made to work with positive numbers only. (Only saying this because I was looking for a similar formatter that deals with neg numbers as well.) – Thomas Tempelmann Apr 11 '13 at 16:08
  • I would like to point out that, 8 years later, this is what `padStart` does, which major browsers support. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart – jrasm91 Apr 05 '21 at 15:21
2

I usually just do something like ("00000000"+nr).slice(-base) where nr is the number and base is the number of digits you want to end up with.

atymic
  • 3,093
  • 1
  • 13
  • 26
DavidMc
  • 21
  • 1
1

One way would be to download sprintf for javascript and writing something like this:

int i = 1;
string s = sprintf("%02d", i);
document.write(s); // Prints "01"
Hubro
  • 56,214
  • 69
  • 228
  • 381
-1
var num =10
document.write(num);

But if your question is to use two digit numbers twice then you can use this

var num1 = 10;
var num2 = 20;
var resultStr = string.concat(num1,num2);
document.write(resultStr);
...
Result: 1020

if you want a space in between,

var resultStr = string.concat(num1,"",num2);

Hope this helps...

Anubhav Ranjan
  • 1,558
  • 3
  • 18
  • 32
  • 1
    I think was op wants is padding the number if it's < 10, so 1 becomes string 01 or 001 – Hubro Jun 02 '11 at 21:46
  • This wont help, as it doesn't address the problem of String formatting. (e.g. the op wants to have something like `i=1 ==> "01"`) – nfechner Jun 02 '11 at 21:46
  • Nope, you didn't understand. Your answer has nothing to do with string padding – Hubro Jun 02 '11 at 21:59
  • @codemonkey: before your first comment came, I was actually modifying the code so couldn't look at it and after saving, the page updated with your comment. So it's not that I have not understood. I have understood it properly. Thanks anyways.... – Anubhav Ranjan Jun 02 '11 at 22:04
  • You still haven't understood it. Your answer still has nothing to do with STRING PADDING. Op was looking for a way to fill out a string with zeros to ensure a fixed length. For instance, 1 becomes "001" – Hubro Jun 03 '11 at 02:21