If I have an output of any double or integer that needs to be rounded to a certain number of decimal places, how would I code that? As always there are many ways to do this but, again, as always, I'll be looking for the simplest way to do such a thing without compensating any viability in my program. Thanks for all the help.
IMtheBESTatJAVA
-11
Light Poster
Recommended Answers
Jump to Posttried using DecimalFormat?
Jump to PostThere is Math.round() that rounds a double:
10.9 --> 11.0
10.4 --> 10.0If you want a certain number of decimal digits you can:
Multiply (for example) the number with 100, then round it with the above method and then divided again with 100:
10.123456 --> 1012.3456 --> …
All 7 Replies
stultuske
1,116
Posting Maven
Featured Poster
javaAddict
900
Nearly a Senior Poster
Team Colleague
Featured Poster
IMtheBESTatJAVA
-11
Light Poster
stultuske
1,116
Posting Maven
Featured Poster
IMtheBESTatJAVA
-11
Light Poster
javaAddict
900
Nearly a Senior Poster
Team Colleague
Featured Poster
orko
36
Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.