Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format numbers by prepending 0 to single-digit numbers?

I want to format a number to have two digits. The problem is caused when 09 is passed, so I need it to be formatted to 0009.

Is there a number formatter in JavaScript?

like image 432
Keith Power Avatar asked Nov 07 '11 21:11

Keith Power


People also ask

How make numbers to 2 digit in JavaScript?

Use the toFixed() method in JavaScript to format a number with two decimals. The toFixed() method formats a number with a specific number of digits to the right of the decimal.

How do you format numbers in JavaScript?

JavaScript Number Format Comma. To format a number with a comma means to separate the number by a comma at every 3rd digit like 123456789 => 1,234,567,890 to be more readable for system million, billion, trillion, etc. Another style of separating number is by placing comma at like 123456789 => 1,23,45,67,890.


2 Answers

Edit (2021):

It's no longer necessary to format numbers by hand like this anymore. This answer was written way-back-when in the distant year of 2011 when IE was important and babel and bundlers were just a wonderful, hopeful dream.

I think it would be a mistake to delete this answer; however in case you find yourself here, I would like to kindly direct your attention to the second highest voted answer to this question as of this edit.

It will introduce you to the use of .toLocaleString() with the options parameter of {minimumIntegerDigits: 2}. Exciting stuff. Below I've recreated all three examples from my original answer using this method for your convenience.

[7, 7.5, -7.2345].forEach(myNumber => {   let formattedNumber = myNumber.toLocaleString('en-US', {     minimumIntegerDigits: 2,     useGrouping: false   })   console.log(     'Input:    ' + myNumber + '\n' +     'Output:   ' + formattedNumber   ) })

Original Answer:

The best method I've found is something like the following:

(Note that this simple version only works for positive integers)

var myNumber = 7; var formattedNumber = ("0" + myNumber).slice(-2); console.log(formattedNumber);

For decimals, you could use this code (it's a bit sloppy though).

var myNumber = 7.5; var dec = myNumber - Math.floor(myNumber); myNumber = myNumber - dec; var formattedNumber = ("0" + myNumber).slice(-2) + dec.toString().substr(1); console.log(formattedNumber);

Lastly, if you're having to deal with the possibility of negative numbers, it's best to store the sign, apply the formatting to the absolute value of the number, and reapply the sign after the fact. Note that this method doesn't restrict the number to 2 total digits. Instead it only restricts the number to the left of the decimal (the integer part). (The line that determines the sign was found here).

var myNumber = -7.2345; var sign = myNumber?myNumber<0?-1:1:0; myNumber = myNumber * sign + ''; // poor man's absolute value var dec = myNumber.match(/\.\d+$/); var int = myNumber.match(/^[^\.]+/);  var formattedNumber = (sign < 0 ? '-' : '') + ("0" + int).slice(-2) + (dec !== null ? dec : ''); console.log(formattedNumber);
like image 89
Joseph Marikle Avatar answered Sep 20 '22 02:09

Joseph Marikle


Use the toLocaleString() method in any number. So for the number 6, as seen below, you can get the desired results.

(6).toLocaleString('en-US', {minimumIntegerDigits: 2, useGrouping:false}) 

Will generate the string '06'.

like image 20
nyteshade Avatar answered Sep 21 '22 02:09

nyteshade