Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a number 0..9 to display with 2 digits (it's NOT a date)

People also ask

How do you format a string to show two decimal places?

String strDouble = String. format("%. 2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.

How do you make a two digit number in JavaScript?

The padStart() method is used on this string with the length parameter given as 2 and the string to be replaced with, given the character '0'. This will format any single digit number to 2 digits by prepending a '0' and leave 2 digit numbers as is.

How do you convert a single digit to a double digit in Java?

format() method, which will let you do exactly what you want: String s = String. format("%02d", someNumber);

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.


You can use:

String.format("%02d", myNumber)

See also the javadocs


If you need to print the number you can use printf

System.out.printf("%02d", num);

You can use

String.format("%02d", num);

or

(num < 10 ? "0" : "") + num;

or

(""+(100+num)).substring(1);

You can use this:

NumberFormat formatter = new DecimalFormat("00");  
String s = formatter.format(1); // ----> 01

The String class comes with the format abilities:

System.out.println(String.format("%02d", 5));

for full documentation, here is the doc