1

I have a String value; and i need to have it always with 2 decimals as a Double type; so, if i get 500, i need to convert it to 500.00 being always a double value.

My problem is when i have something likexxx.00 or xxx.x0, the parseDouble method removes the second 0.

My code is something like this

String number = "500.00";
Double numberToSave = (Double.ParseDouble(number));

And numberToSave returns 500.0

Any ideas of how can i resolve this? I need to show the second decimal even if it is 0.

Thank you.

SeniorJD
  • 6,946
  • 4
  • 36
  • 53
  • 6
    You're confusing the **formatting** of the value with its actual value. – Bob Kaufman Jun 24 '13 at 14:20
  • 2
    500, 500.0, and 500.00 are all the same number. The issue isn't with `Double.ParseDouble`. You should be worrying about how to properly format the number when displaying the value to users, not storing it. – Justin Niessner Jun 24 '13 at 14:20
  • See this lik: http://stackoverflow.com/questions/17236335/problems-with-decimalformat/17236468#17236468 – Masudul Jun 24 '13 at 14:21
  • 1
    possible duplicate of http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java – Marco Forberg Jun 24 '13 at 14:22

5 Answers5

5

Double doesn't have any concept of maintaining insignificant digits.

The problem isn't the Double (or double) value you're using - it's how you then convert it back into a string. You should use a DecimalFormat which specifies the appropriate number of decimal places.

Also note that you're currently using Double.parseDouble which returns a double, but you're assigning it to a Double variable, which will box the value - potentially unnecessarily.

Sample code:

import java.text.DecimalFormat;

public class Test {
    public static void main(String[] args) {
        String text = "500.00";
        double number = Double.parseDouble(text);

        DecimalFormat format = new DecimalFormat("0.00");
        String formatted = format.format(number);
        System.out.println(formatted);
    }
}

Note that the above code will give you two decimal digits even if there are more digits available.

Additionally, if exact decimal digits matter to you, you might want to consider using BigDecimal instead of double. This is particularly important if your values actually represent currency. You should understand the difference between a floating binary point type (such as double) and a floating decimal point type (such as BigDecimal) and choose appropriately between them.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Sorry Jon. It was a mistake. I added the java docs link in your answer and removed it too. – JHS Jun 24 '13 at 14:23
  • Thank you, it was useful, you were absoluteli right, the problem was not the Double itself, it was the logic i was using. So i have modified the code adding a String var that gets the number and format it so i can show it as i need. – Sebastián Aguerre Jun 24 '13 at 14:47
2

There's always String.format as well,

String.format("%.2f", d)

If you're only worried about printing the formatted value, you can use System.out.printf,

double d = 500;
System.out.printf("%.2f%n", d);  // %n is a line-break
500.00
arshajii
  • 127,459
  • 24
  • 238
  • 287
1

You need to use the DecimalFormat class.

Something like this DecimalFormat df = new DecimalFormat("#.##");.

Have a look at the java docs.

JHS
  • 7,761
  • 2
  • 29
  • 53
  • Where by "something like" you mean "not actually #.## as the pattern"? (That would just give 500 for the sample value.) – Jon Skeet Jun 24 '13 at 14:28
  • Yeah. I thought I'd let the questioner think a little. ;) – JHS Jun 24 '13 at 14:32
  • But there's a difference between that and writing something that simply does not do what the question asks for. – arshajii Jun 24 '13 at 14:36
  • arshajii - Can you help me understand what does "Something like this" means. I thought it means "a hint". Do you want questioners to be spoon fed? – JHS Jun 24 '13 at 14:39
  • 1
    Thanks Junaid, i have read about the format and the #; it helped me to learn a little more. – Sebastián Aguerre Jun 24 '13 at 14:49
1

You might want to look into the DecimalFormat class, it can be used to format a doubles like this:

import java.text.*;

public class FormatDoubleTest
{
    public static void main(String[] args)
    {
        double d1 = 500.00;
        double d2 = 500.001;
        double d3 = 500.009;

        DecimalFormat df = new DecimalFormat("0.00");

        System.out.println(df.format(d1));
        System.out.println(df.format(d2));
        System.out.println(df.format(d3));

    }
}
FrustratedWithFormsDesigner
  • 26,726
  • 31
  • 139
  • 202
0

Use a NumberFormat object.

String number = "500.00";
Double numberToSave = (Double.ParseDouble(number));
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(3);
System.out.println(nf.format(numberToSave));
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70