How do I round?

Reply

Join Date: Oct 2008
Posts: 37
Reputation: IMtheBESTatJAVA has a little shameless behaviour in the past 
Solved Threads: 0
IMtheBESTatJAVA IMtheBESTatJAVA is offline Offline
Light Poster

How do I round?

 
0
  #1
Oct 20th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: How do I round?

 
0
  #2
Oct 20th, 2008
tried using DecimalFormat?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,658
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 224
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: How do I round?

 
0
  #3
Oct 20th, 2008
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
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 37
Reputation: IMtheBESTatJAVA has a little shameless behaviour in the past 
Solved Threads: 0
IMtheBESTatJAVA IMtheBESTatJAVA is offline Offline
Light Poster

Re: How do I round?

 
0
  #4
Oct 21st, 2008
So exactly how would I write this? Would I type in the variable I would like rounded and then Math.round(), or visa versa?
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: How do I round?

 
0
  #5
Oct 21st, 2008
take a look at the Math api's
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 37
Reputation: IMtheBESTatJAVA has a little shameless behaviour in the past 
Solved Threads: 0
IMtheBESTatJAVA IMtheBESTatJAVA is offline Offline
Light Poster

Re: How do I round?

 
0
  #6
Oct 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,658
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 224
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: How do I round?

 
0
  #7
Oct 22nd, 2008
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
  1. double d = Math.round(10.9);
  2. System.out.println(d); //will print 11.0
  3.  
  4.  
  5. double d = Math.round(10.4);
  6. System.out.println(d); //will print 10.0
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 164
Reputation: orko is an unknown quantity at this point 
Solved Threads: 10
orko orko is offline Offline
Junior Poster

Re: How do I round?

 
0
  #8
Oct 22nd, 2008
Originally Posted by IMtheBESTatJAVA View Post
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.
Your nick is IMtheBESTatJAVA and you are telling us you are just beginner? Good sarcasm. References are complicated sometimes, but most of the cases it appears to be complicated because of lack of patience.
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC