How to append 0 to before an integer digit in java.? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to append 0 to before an integer digit in java.?

For ex: I need convert 9 as 09. (I done with converting strings, searching is there any way Without converting to strings.) How to do it? Any one have an idea....

23rd Dec 2019, 9:08 AM
Jayakrishna 🇮🇳
5 Answers
+ 1
Coder Kitten printf version not suitable for my program because am not printing individually. Am finding any alternatives.. What is argument to String. format ()?
23rd Dec 2019, 9:21 AM
Jayakrishna 🇮🇳
+ 1
Let y be 19, and x be 20 How do we prepend x before y? Multiply x by 10 raised to the power of [digits-in-y] 20 * (10 ** 2) 20 * 100 -> 2000 Now we can prepend x before y, and got 2000 + 19 -> 2019 What if x was zero? try the same approach, and it just doesn't work 0 * (10 ** 2) 0 * 100 -> 0 0 + 19 -> 19 Zero filling a number is about output formatting, it doesn't alter the original number as I see it. But It's possible that I misunderstood about it, no surprise : )
23rd Dec 2019, 10:12 AM
Ipang
+ 1
In a program I wrote some days ago, I needed to assign number but when if it is single digit, I converted to String and append 0. Like in timer, I displayed 09:00:00. But I didn't got otherway. Question raised but not much thought about it. So i thought it, is there a way other than String version? because in date class it display in numbers, internally is it performs on numbers or on strings at end? So thinking is there any way or methods internally it performing? So It seems not possible, other than outputting or strings... So anyway thanks Coder Kitten, Ipang .. For your response...
23rd Dec 2019, 10:36 AM
Jayakrishna 🇮🇳
+ 1
Jayakrishna String formatting is not the same as typecasting an integer/float into a string. instead it generates a string containing your integer/float variables in the string format specified. The alternative scenario Coder Kitten refers to simply has one variable containing the whole time object as seconds: 60 = 0h 1m 0s 3661 = 1h 1m 1s etc. a famous example that can be utilized in most programming languages is the Unix timestamp, which refers to how much time (in seconds) has passed since 01-01-1970 00:00:00 (equals Unix timestamp value 0). you could use string formatting for the output as well. while the unix timestamp is harder to immediately understand for a human, it is actually much easier for your computer to perform arithmetics on a single variable so the programming is much more convenient, and computing power is used much more efficiently. edit: one more thing. putting something in front of another thing is called prepending. appending is the opposite. 😉
23rd Dec 2019, 7:28 PM
grdr
0
I used that toString method only.. But finding other than that, is available or not. so date method also uses toString method. But I don't understood your last para about alternative scinario... Coder Kitten
23rd Dec 2019, 4:26 PM
Jayakrishna 🇮🇳