39

I'd like to convert a BigDecimal to String for printing purposes but print out all digits without scientific notation. For example:

BigDecimal d = BigDecimal.valueOf(12334535345456700.12345634534534578901);
String out = d.toString(); // Or perform any formatting that needs to be done
System.out.println(out);

I'd like to get 12334535345456700.12345634534534578901 printed. Right now I get: 1.23345353454567E+16.

user202729
  • 2,782
  • 3
  • 19
  • 30
Mensur
  • 1,136
  • 3
  • 16
  • 29
  • `System.out.println(out);` not `System.out.println(output);` – Iswanto San Apr 05 '13 at 13:16
  • 4
    Part of the problem is `12334535345456700.12345634534534578901` is truncated to `1.23345353454567E+16` even before the value is passed into the `BigDecimal` constructor, because constant double values in your code don't have that much precision. You can verify this by simply trying to store it in a `double` variable instead and printing it out: http://ideone.com/j5FKp1. You need to start with the value as a string if you want the extra precision. – mellamokb Apr 05 '13 at 13:17

4 Answers4

60

To preserve the precision for a BigDecimal you need to pass the value in as a String

BigDecimal d = new BigDecimal("12334535345456700.12345634534534578901");
System.out.println(d.toPlainString());
Reimeus
  • 155,977
  • 14
  • 207
  • 269
22

The BigDecimal class has a toPlainString method. Call this method instead of the standard toString and it will print out the full number without scientific notation.

Example

BigDecimal b = new BigDecimal("4930592405923095023950238502395.3259023950235902");
System.out.println(b.toPlainString());

Output: 4930592405923095023950238502395.3259023950235902
christopher
  • 25,939
  • 5
  • 53
  • 88
3

You want to use a DecimalFormat:

DecimalFormat df = new DecimalFormat("#.#");  
String output = df .format(myBD);
System.out.println(value + " " + output);
Woot4Moo
  • 23,491
  • 13
  • 90
  • 146
-1

You could use this

BigDecimal d = BigDecimal.valueOf(12334535345456700.12345634534534578901);
String out= d.toPlainString();
System.out.println(out);
Waqas Memon
  • 1,189
  • 8
  • 22
  • 2
    -1 The problem is in the precision of the original number being passed as a double. You could at least do your due diligence and test this with ideone, which takes all of 30 seconds: http://ideone.com/n137Cs – mellamokb Apr 05 '13 at 13:19
  • how come did you accept that new BigDecimal(String value); from Reimeus can work? i guess BigDecimal.valueOf does not accept Strings in constructor? does it? – Waqas Memon Apr 05 '13 at 13:24
  • Right, I believe `valueOf` is only for passing in numeric values. You will need to use the constructor to pass in a string value. – mellamokb Apr 05 '13 at 13:25
  • BigDecimal d = BigDecimal.valueOf("12334535345456700.12345634534534578901"); //is a compile time error The method valueOf(long) in the type BigDecimal is not applicable for the arguments (String) . . BigDecimal d = new BigDecimal("12334535345456700.12345634534534578901"); //correct one.. – Waqas Memon Apr 05 '13 at 13:25