How to print integer value in 2 digits? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to print integer value in 2 digits?

I mean Edith is 2,like 01,03,45,99 etc .I want o/p like this can anyone explain

29th Jan 2017, 3:13 AM
kumar G
kumar G - avatar
3 Answers
0
Maybe you can check if the digit is lower than 10, then it will print 0 before the digit.
29th Jan 2017, 3:16 AM
Dawzy
Dawzy - avatar
0
Yea I tried this printing 0 ,but 0 is a string and string cannot be compatible with integer
29th Jan 2017, 3:18 AM
kumar G
kumar G - avatar
0
You can use System.out.printf(String format, Object... args). You give it one formatted String and the values used separated by comma. Example: System.out.printf("%02d", x); %d stands for an integer, %2d means it'll make the printed value at least two characters long and fills up with spaces if needed, %02d will do the same as %2d but will fill up with '0' instead. More information to printf (and probably a better explanation than mine :P): https://docs.oracle.com/javase/tutorial/java/data/numberformat.html It's mainly about the format function but printf works the same way.
29th Jan 2017, 3:38 AM
Robobrine
Robobrine - avatar