2

I tried many ways to convert a Double to it's exact decimal String with String.format() and DecimalFormater and I found this so far :

String s = "-.0000000000000017763568394002505";
Double d = Double.parseDouble(s) * 100;
System.out.println((new java.text.DecimalFormat("###########################.################################################################################")).format(d));

(https://ideone.com/WG6zeC)

I'm not really a fan of my solution because it's ugly to me to make a huge pattern that does not covers all the cases.

Do you have another way to make this ?
I really want to have ALL the decimals, without scientific notation, without trailing zeros.

Thanks

P.S.: I need this without external libraries and in Java 1.7.

pierreonthenet
  • 155
  • 1
  • 12
  • 2
    did you try using BigDecimal? – mlecz Feb 15 '18 at 10:22
  • 1
    Never use float or double types if you want *exact* decimal values. Read [Is floating point math broken?](https://stackoverflow.com/q/588004/3545273) for more... – Serge Ballesta Feb 15 '18 at 10:25
  • 1
    Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Serge Ballesta Feb 15 '18 at 10:26
  • Nop! Nothing to do with broken floating point : this problem is in the convertion from Double to String... – pierreonthenet Feb 15 '18 at 13:50
  • To be clear, are you seeking to print out the full precision of the double exactly as-is after having been stored to a double, or do you want to print the double out as an exact decimal representation of whatever the input was? People seem to have assumed you are asking for the latter so far, but my guess would be you're asking about the former, and the two have very different answers. – Tim M. Feb 15 '18 at 17:35
  • I'm not really sure to understand what you are saying, so I'll tell what I want in another way : I have a String that represents a decimal number and I want to 1/ multiply this decimal by 100 ; 2/ print this decimal as a decimal without the scientific notation. Exemple : I get "-.0000000000000017763568394002505", I multiply it by 100, I should have "-.00000000000017763568394002505" or "-0.00000000000017763568394002505" (no matter the zero before the floating point). – pierreonthenet Feb 19 '18 at 09:21

3 Answers3

4

Seems as if you have to deal with some precision. double is not intended for this use. Better use BigDecimal

String value="13.23000";
BigDecimal bigD=new BigDecimal(value);
System.out.println(bigD);
System.out.println(bigD.stripTrailingZeros().toPlainString());
Yati Sawhney
  • 1,372
  • 1
  • 12
  • 19
  • Thank you, but this doesn't work because I get 2 trailing zeros : `String s = "-.0000000000000017763568394002505"; BigDecimal bd = new BigDecimal(s); bd = bd.multiply(new BigDecimal(100)); System.out.println(bd.toPlainString());` Returns : "-0.0000000000001776356839400250500" – pierreonthenet Feb 15 '18 at 16:57
1

There is a big difference between new BigDecimal("0.1") and new BigDecimal(0.1). Using the String constructor can result in trailing zeros, and loses the effects of rounding to double on parsing the string. If the objective is to print the exact value of the double, you need to use BigDecimal's double constructor. Do any arithmetic in double, not BigDecimal.

import java.math.BigDecimal;

public class Test {
  public static void main(String[] args) {
    String s = "-.0000000000000017763568394002505";
    double d = Double.parseDouble(s) * 100;
    System.out.println(new BigDecimal(d).toPlainString());
  }
}
Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
  • Thank you, but as you can see here this doesn't work : https://ideone.com/cwFuqI I get "-0.00000000000017763568394002504646778106689453125" but I should have "-0.00000000000017763568394002505"... – pierreonthenet Feb 19 '18 at 09:11
  • @pierreonthenet It depends on which you want, the original string or the exact value of the double. There is no IEEE 754 64-bit binary float whose value is 0.00000000000017763568394002505. – Patricia Shanahan Feb 19 '18 at 09:56
  • @pierreonthenet If exact decimal values are really what you want, consider using BigDecimal instead of double as your primary arithmetic type. – Patricia Shanahan Feb 19 '18 at 10:25
  • Thank you ! So there is a link to "Is floating point math broken?", but it's a side-effect on what I wanted to do. – pierreonthenet Feb 19 '18 at 13:45
0

Try this code: You can also use toString() method, but it uses scientific notation. https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#toPlainString()

BigDecimal bd = new BigDecimal("-.0000000000000017763568394002505");
System.out.println(bd.toPlainString());
mlecz
  • 985
  • 11
  • 19
  • Thank you, but this doesn't work because I get 2 trailing zeros : `String s = "-.0000000000000017763568394002505"; BigDecimal bd = new BigDecimal(s); bd = bd.multiply(new BigDecimal(100)); System.out.println(bd.toPlainString());` – pierreonthenet Feb 15 '18 at 11:07