Number Rounding

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2005
Posts: 31
Reputation: AQWst is an unknown quantity at this point 
Solved Threads: 0
AQWst AQWst is offline Offline
Light Poster

Number Rounding

 
0
  #1
Mar 13th, 2005
Can someone help me create a rounding routine? I have some mathematical calculations being done in a program that the final result is a double. The only trouble is, is when I want to display it, there are too many digits appearing. I have searched the Sun web site and others without finding anything that will help.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Number Rounding

 
0
  #2
Mar 14th, 2005
You should really dig into the API docs a bit. Depending on what you want to achieve you can use one of two classes at least to achieve what you want using only standard API functions.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Number Rounding

 
0
  #3
Mar 14th, 2005
Yep, you can use decimal format, or the Math.round(), but you'll be satisfied with the DecimalFormat class.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: Number Rounding

 
0
  #4
Mar 14th, 2005
  1. import java.text.DecimalFormat;
  2.  
  3. public class DecimalTest
  4. {
  5. public static void main(String[] args)
  6. {
  7. DecimalFormat dfMoney = new DecimalFormat("$###.##");
  8. DecimalFormat df2 = new DecimalFormat("###.##");
  9. DecimalFormat df3 = new DecimalFormat("###.###");
  10. DecimalFormat df4 = new DecimalFormat("###.####");
  11.  
  12. System.out.println(dfMoney.format(294.489003));
  13. System.out.println(df2.format(294.489003));
  14. System.out.println(df3.format(5935.393432));
  15. System.out.println(df4.format(1323.23423));
  16.  
  17. }
  18. }

Here is the output:
$294.49
294.49
5935.393
1323.2342
Reply With Quote Quick reply to this message  
Reply

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




Views: 12093 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC