I want to convert 10.5 to 10.500 in Java. How can I do it??
Asked
Active
Viewed 73 times
0
-
Is this for further calculation or is it for printing out the value? Can we presume the latter? – Maarten Bodewes Jan 08 '20 at 17:32
-
Are you sure you want to convert 10.5 to 105? Is that just a typo? – computercarguy Jan 08 '20 at 17:33
-
2This shows that you should reread your question before posting it, I'd also create smaller titles and use more tags. Also, what *exactly* do you mean with "up" in "round **up**"? – Maarten Bodewes Jan 08 '20 at 17:34
-
This might be a [dupe](https://stackoverflow.com/q/16583604/589259) although it kind of presumes knowing about `DecimalFormat`, and it doesn't strictly round **up** it seems to me. – Maarten Bodewes Jan 08 '20 at 17:39
-
“I want to convert 10.5 to 10.500 in Java. How can I do it?” By doing nothing. They are numerically identical. – VGR Jan 08 '20 at 19:17
1 Answers
2
NumberFormat formatter = new DecimalFormat("#0.000");
System.out.println(formatter.format(10.5));

Maxdola
- 1,562
- 2
- 10
- 29
-
OP updated their Q to fix a typo. You might want to adjust your A to fit now. – computercarguy Jan 08 '20 at 17:35
-
Might want to call `formatter.setRoundingMode(RoundingMode.UP)`, since question title starts with *"How can I **round up** ..."*, though OP probably didn't mean it like that, but at least mention how to change the rounding mode. – Andreas Jan 08 '20 at 17:54