943,937 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 18932
  • Java RSS
Mar 13th, 2005
0

Number Rounding

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
AQWst is offline Offline
31 posts
since Jan 2005
Mar 14th, 2005
0

Re: Number Rounding

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Mar 14th, 2005
0

Re: Number Rounding

Yep, you can use decimal format, or the Math.round(), but you'll be satisfied with the DecimalFormat class.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Mar 14th, 2005
0

Re: Number Rounding

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 13
Solved Threads: 4
Posting Whiz
paradox814 is offline Offline
351 posts
since Oct 2004

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: Adding components to JApplet
Next Thread in Java Forum Timeline: Password Field in a JOptionPane





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


Follow us on Twitter


© 2011 DaniWeb® LLC