• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1200
  • Last Modified:

Rounding a currency value to the nearest dollar

I want to know how to take a double, which I am currently formatting with NumberFormat using the format() method to make into currency, and chop off the cents by rounding up to the nearest whole number.  I tried a Math.round() on the double, but it still displays something like $123.00 instead of $123.  How can you take a double, round it up, and convert it into a currency format?

0
gdlp2004
Asked:
gdlp2004
  • 4
1 Solution
 
CEHJCommented:
Quick and dirty:

            NumberFormat nf = new DecimalFormat("0");
                  double d = 123.123;
                  System.out.println(nf.format(d));
                  
0
 
CEHJCommented:
Cleaner:

                  nf = NumberFormat.getCurrencyInstance();
                  nf.setMaximumFractionDigits(0);
0
 
CEHJCommented:
If you want dollars irrespective of Locale:

nf = NumberFormat.getCurrencyInstance(Locale.US);
0
 
gdlp2004Author Commented:
Thanks man, you rock. :)
0
 
CEHJCommented:
:-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now