66

I have a BigDecimal object and i want to convert it to string. The problem is that my value got fraction and i get a huge number (in length) and i only need the original number in string for example: for

BigDecimal bd = new BigDecimal(10.0001)
System.out.println(bd.toString());
System.out.println(bd.toPlainString());

the output is:

10.000099999999999766941982670687139034271240234375
10.000099999999999766941982670687139034271240234375

and i need the out put to be exactly the number 10.0001 in string

Bhavik Ambani
  • 6,357
  • 14
  • 52
  • 84
Yo Al
  • 1,959
  • 4
  • 19
  • 21

8 Answers8

128

To get exactly 10.0001 you need to use the String constructor or valueOf (which constructs a BigDecimal based on the canonical representation of the double):

BigDecimal bd = new BigDecimal("10.0001");
System.out.println(bd.toString()); // prints 10.0001
//or alternatively
BigDecimal bd = BigDecimal.valueOf(10.0001);
System.out.println(bd.toString()); // prints 10.0001

The problem with new BigDecimal(10.0001) is that the argument is a double and it happens that doubles can't represent 10.0001 exactly. So 10.0001 is "transformed" to the closest possible double, which is 10.000099999999999766941982670687139034271240234375 and that's what your BigDecimal shows.

For that reason, it rarely makes sense to use the double constructor.

You can read more about it here, Moving decimal places over in a double

assylias
  • 297,541
  • 71
  • 621
  • 741
  • A more efficient way of converting a `double` this way is to use `BigDecimal.valueOf(double);` – Peter Lawrey Dec 16 '12 at 10:19
  • @PeterLawrey Not sure I understand. `BigDecimal.valueOf(double)` "rounds" the double in the same way doubles get rounded when printed, which uses somewhat complex rules so I'm not sure how you can rely on that. – assylias Dec 16 '12 at 10:24
  • `Translates a double into a BigDecimal, using the double's canonical string representation provided by the Double.toString(double) method. Note: This is generally the preferred way to convert a double (or float) into a BigDecimal, as the value returned is equal to that resulting from constructing a BigDecimal from the result of using Double.toString(double).` – Peter Lawrey Dec 16 '12 at 10:29
  • @PeterLawrey Yes I know that. I just don't know how predictable it is that `Double.toString(double)` will return the expected result. For example, `Double.toString(99999999.21110999)` returns `99999999.21111`... – assylias Dec 16 '12 at 10:36
  • That is true but in that situation using the constructor won't help either, you have to use rounding. – Peter Lawrey Dec 16 '12 at 10:42
  • this answer dosent really answer the question as to how to convert a BigDecimal to a String. – Tim Boland Dec 14 '14 at 02:55
12

Your BigDecimal doesn't contain the number 10.0001, because you initialized it with a double, and the double didn't quite contain the number you thought it did. (This is the whole point of BigDecimal.)

If you use the string-based constructor instead:

BigDecimal bd = new BigDecimal("10.0001");

...then it will actually contain the number you expect.

reevesy
  • 3,354
  • 1
  • 24
  • 22
T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
8

By using below method you can convert java.math.BigDecimal to String.

   BigDecimal bigDecimal = new BigDecimal("10.0001");
   String bigDecimalString = String.valueOf(bigDecimal.doubleValue());
   System.out.println("bigDecimal value in String: "+bigDecimalString);

Output:
bigDecimal value in String: 10.0001

Saminathan
  • 91
  • 1
  • 2
8

For better support different locales use this way:

DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
df.setMinimumFractionDigits(0);
df.setGroupingUsed(false);

df.format(bigDecimal);

also you can customize it:

DecimalFormat df = new DecimalFormat("###,###,###");
df.format(bigDecimal);
Atetc
  • 1,448
  • 17
  • 24
1
// Convert BigDecimal number To String by using below method //

public static String RemoveTrailingZeros(BigDecimal tempDecimal)
{
    tempDecimal = tempDecimal.stripTrailingZeros();
    String tempString = tempDecimal.toPlainString();
    return tempString;
}

// Recall RemoveTrailingZeros
BigDecimal output = new BigDecimal(0);
String str = RemoveTrailingZeros(output);
Rzgar
  • 11
  • 2
0

The BigDecimal can not be a double. you can use Int number. if you want to display exactly own number, you can use the String constructor of BigDecimal .

like this:

BigDecimal bd1 = new BigDecimal("10.0001");

now, you can display bd1 as 10.0001

So simple. GOOD LUCK.

Dimitri
  • 7,489
  • 18
  • 63
  • 117
Sajad
  • 2,016
  • 10
  • 44
  • 81
0

To archive the necessary result with double constructor you need to round the BigDecimal before convert it to String e.g.

new java.math.BigDecimal(10.0001).round(new java.math.MathContext(6, java.math.RoundingMode.HALF_UP)).toString()

will print the "10.0001"

foal
  • 526
  • 4
  • 18
0

If you just need to set precision quantity and round the value, the right way to do this is use it's own object for this.

BigDecimal value = new BigDecimal("10.0001");
value = value.setScale(4, RoundingMode.HALF_UP);
System.out.println(value); //the return should be "10.0001"

One of the pillars of Oriented Object Programming (OOP) is "encapsulation", this pillar also says that an object should deal with it's own operations, like in this way:

Sham Fiorin
  • 164
  • 2
  • 11