943,844 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 14519
  • Java RSS
Oct 20th, 2008
0

How do I round?

Expand Post »
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.
Similar Threads
Reputation Points: -1
Solved Threads: 0
Light Poster
IMtheBESTatJAVA is offline Offline
36 posts
since Oct 2008
Oct 20th, 2008
0

Re: How do I round?

tried using DecimalFormat?
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is online now Online
2,497 posts
since Jan 2007
Oct 20th, 2008
0

Re: How do I round?

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
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Oct 21st, 2008
0

Re: How do I round?

So exactly how would I write this? Would I type in the variable I would like rounded and then Math.round(), or visa versa?
Reputation Points: -1
Solved Threads: 0
Light Poster
IMtheBESTatJAVA is offline Offline
36 posts
since Oct 2008
Oct 21st, 2008
0

Re: How do I round?

take a look at the Math api's
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is online now Online
2,497 posts
since Jan 2007
Oct 22nd, 2008
0

Re: How do I round?

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.
Reputation Points: -1
Solved Threads: 0
Light Poster
IMtheBESTatJAVA is offline Offline
36 posts
since Oct 2008
Oct 22nd, 2008
0

Re: How do I round?

That reference sheet has all the methods you will need as well as what they do and how they are called.

Quote ...
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)
  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
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Oct 22nd, 2008
0

Re: How do I round?

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
Reputation Points: 46
Solved Threads: 11
Junior Poster
orko is offline Offline
164 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Oval drawn is jagged instead of smooth
Next Thread in Java Forum Timeline: class to read any kind of file format e.g .doc,.pdf,.txt





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC