| | |
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 |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character chat class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list loops mac map method methods mobile netbeans newbie notdisplaying number online printf problem program programming project properties qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor






