| | |
How do I round?
![]() |
•
•
Join Date: Oct 2008
Posts: 37
Reputation:
Solved Threads: 0
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.
There is Math.round() that rounds a double:
10.9 --> 11.0
10.4 --> 10.0
If 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 --> 1012.00 --> 10.12
Or :
java.text package
that has this:
DecimalFormat
10.9 --> 11.0
10.4 --> 10.0
If 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 --> 1012.00 --> 10.12
Or :
java.text package
that has this:
DecimalFormat
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Oct 2008
Posts: 37
Reputation:
Solved Threads: 0
Well all I need to do is make a double like 23.5 to be 23.50 and longer ones like 23.556 to be 23.55 or 23.56. Would I do this as doublevalue = Math.round(2);? I seriously just started programming with java so sending me to those elongated complicated reference sheets won't do too much help as I've only learned basic coding and algorithmic functions in Java.
That reference sheet has all the methods you will need as well as what they do and how they are called.
•
•
•
•
There is Math.round() that rounds a double:
10.9 --> 11.0
10.4 --> 10.0
If 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 --> 1012.00 --> 10.12
Java Syntax (Toggle Plain Text)
double d = Math.round(10.9); System.out.println(d); //will print 11.0 double d = Math.round(10.4); System.out.println(d); //will print 10.0
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Apr 2006
Posts: 164
Reputation:
Solved Threads: 10
•
•
•
•
Well all I need to do is make a double like 23.5 to be 23.50 and longer ones like 23.556 to be 23.55 or 23.56. Would I do this as doublevalue = Math.round(2);? I seriously just started programming with java so sending me to those elongated complicated reference sheets won't do too much help as I've only learned basic coding and algorithmic functions in Java.
Math.round() takes the number you want to roundup. That won't give you any decimal. After you round the number, if you divide it by 100.0, that shifts the number to two decimal points.
I am referring you to this for more explanation: http://www.daniweb.com/forums/thread32513.html
A Perfect World
![]() |
Similar Threads
- round robin algorithm (Computer Science)
- Round Robin Algorithm Simulation (C++)
- Round Robin algorithm? (Java)
Other Threads in the Java Forum
- Previous Thread: Oval drawn is jagged instead of smooth
- Next Thread: class to read any kind of file format e.g .doc,.pdf,.txt
| Thread Tools | Search this Thread |
addball android api append applet application apps array arrays automation awt binary bluetooth businessintelligence busy_handler(null) button card class client code collision component constructor crashcourse css csv database draw eclipse ee error eventlistener exception fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me java javaarraylist javadoc javafx javamicroeditionuseofmotionsensor javaprojects jni jpanel jtree julia jvm linux list loan machine map method methods migrate mobile netbeans oracle output phone physics plazmic problem program programming project radio recursion reporting scanner server service set sharepoint smart sms software sort sortedmaps sql string swing textfield threads tree trolltech unlimited utility webservices windows






