Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-26-2007, 07:35 PM
Member
 
Join Date: Nov 2007
Posts: 3
javaMike is on a distinguished road
rounding double to two decimal places
is there an easy way to round a double to two decimal places?

i.e. 1.98999 would round to 1.99

the math.round method only rounds to nearest integer.

decimalformat class converts number to a string.

i figure there must be an easy way to do this...?? pls advise
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-27-2007, 05:58 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
DecimalFormat really is the easiest option to use:
Code:
double roundTwoDecimals(double d) { DecimalFormat twoDForm = new DecimalFormat("#.##"); return Double.valueOf(twoDForm.format(d)); }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-20-2008, 08:25 PM
Member
 
Join Date: Aug 2008
Posts: 1
shalomshachne is on a distinguished road
Thanks for help with that...
Thanks for your help with that post. Am I insane, or does anyone else think that it's crazy not to have a simple already built function in Java to round a double to specified # of decimal places?

Am I missing something? Why would everyone want to custom code this function?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-20-2008, 08:39 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Gone to Costa Rica
Posts: 2,223
Norm is on a distinguished road
Another way to change the value of the double, you'd need to do something like this:
double dbl = 12.3456;
int ix = (int)(dbl * 100.0); // scale it
double dbl2 = ((double)ix)/100.0;
System.out.println("dbl=" + dbl + ", dbl2=" + dbl2); //dbl=12.3456, dbl2=12.34

To round, you'd need to add in some value.

Last edited by Norm : 08-20-2008 at 08:41 PM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-21-2008, 12:50 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 462
fishtoprecords is on a distinguished road
You are not, by chance, violating the number one rule, are you:

Never use float or double for money. Its a sin.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-21-2008, 01:59 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 810
Fubarable is on a distinguished road
Quote:
Never use float or double for money. Its a sin.
I can't disagree with you as you know a heck of a lot more Java than I'll ever know, but do you think this rule needs to be rigidly enforced for folks taking intro to Java courses?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 08-21-2008, 02:02 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 462
fishtoprecords is on a distinguished road
I think teaching the use of float/double for currency is a mortal sin.

Sadly, some teachers don't know any better.

On this, and other forums, you see folks just starting out, clearly doing homework, and they all use floats or doubles.

The real problem is that it works for wimpy testing, and when you put it in production, stuff doesn't add up, the accountants get very mad, and you have a real mess to fix.

Cobol had fixed point built in from day one. For a reason.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 08-21-2008, 03:32 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 810
Fubarable is on a distinguished road
Again, can't argue with any of that. As you state though, most teachers don't know any better, and most students here are coding with constraints set by these same teachers.

Again, thanks for posting your thoughts on this.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java calculator decimal cart1443 New To Java 2 04-16-2008 03:19 PM
How do I convert a decimal value to hexadecimal with double precision (64 bit) SKaur New To Java 7 01-12-2008 11:02 PM
Capping decimal places Rageagainst20 New To Java 1 12-20-2007 11:28 PM
round to two decimal places javaMike New To Java 1 11-26-2007 11:46 PM
Help with java Rounding silvia New To Java 1 07-20-2007 09:25 AM


All times are GMT +3. The time now is 11:17 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org