6

There is a good question on rounding decimals in Java here. But I was wondering how can I include the trailing zeros to display prices in my program like: $1.50, $1.00

The simple solution of

String.format("%.2g%n", 0.912385);

works just fine, but omits the trailing zero if it is at the last decimal place. The issue comes up in my program even though I only use expressions like this:

double price = 1.50;

When I do calculations with different prices (add, multiply, etc.) the result is primarily displayed like this:

$2.5000000000000003

So, using the String.format works fine for this purpose, but it truncates the above example to

$2.5

Is there a proper way to show the trailing zero at the second decimal place? Or both zeros if the output of a calculation should be

$2.00
Community
  • 1
  • 1
denchr
  • 4,142
  • 13
  • 48
  • 51
  • 4
    As a rule of thumb, never use floating point numbers to represent currency. Instead consider using an integer for the cents / pence. – Pool Nov 14 '09 at 14:07

5 Answers5

20

I would recommend that you do this:

NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
double price = 2.50000000000003;
System.out.println(currencyFormatter.format(price));

This has the virtue of be locale-specific as well. This will work, for example, if you're in the euro zone instead of the US.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Thanks, I was looking for something like that! I didn't know of the getCurrencyInstance() method. – denchr Nov 14 '09 at 14:11
14

While others answers are perfectly valid (especially duffymo's one), you have a much bigger problem than formatting the display. You are actually using a totally wrong type for a money amount which is a discrete value. Instead of a double, you should really consider using a java.lang.BigDecimal.

(EDIT: ... or a well implemented Money class as duffymo pointed out in a comment, for example classes from JScience's monetary module or the more recent Joda-Money - which has still to be released as an official version.)

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
11

It can probably be done with String.format(...), but you could use DecimalFormat:

double price = 2.50000000000003;
DecimalFormat formatter = new DecimalFormat("$0.00");
System.out.println(formatter.format(price)); // print: $2.50
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
4

Have you tried:

String s = String.format("$%.2f", 2.50);

That will do the trick.

dustmachine
  • 10,622
  • 5
  • 26
  • 28
  • Although my answer accomplishes what you requested, the better solution, as others have noted, is to use `BigDecimal` or a true Money class. – dustmachine Sep 08 '11 at 14:30
0
public static String getFormattedData(String actualString) 
    {
        String subString = "0.0";

        if(actualString.contains("."))
        {   
            subString = actualString.substring(actualString.indexOf("."), actualString.trim().length()); 

            if(Double.parseDouble(subString) > 0.0)
                return actualString;
            else
                actualString = actualString.substring(0, actualString.indexOf("."));
        }
        return actualString;
    }
David
  • 19,577
  • 28
  • 108
  • 128
Shiva
  • 1