-2

Following code prints 12113411122222,545000:

Double value = 12113411122222.545421d;
System.out.println(String.format("%f", value));

How to make exact value that the variable value was assigned to to be printed - 12113411122222.545421?

Update: I should have said this at the beginning - I know that BigDecimal needs to be used when precision is needed. However I am using a library which reads a certain type of file and outcome of that is that I get a bunch of attributes from that file. One of the attributes is passed to me from the 3rd party library as a double. After that I am sending attribute value to database which results in attribute value being saved as 1.2113411122222545E13 because value is being converted to String. So now I am looking for a solution how to 'lose' exponent part and making assumption: if Double.MAX_VALUE is 1.7976931348623157E308 then it means I might get attribute value with lots of decimal numbers, for example 12113411122222.545421121212212d, and how do I convert that Double to String without 'loosing' any numbers. I suspect this is an issue with Double's precision but it is counter-intuitive because Double.MAX_VALUE is 1.7976931348623157E308. I guess a better knowledge of inner works of Java and numbers in computer systems in general is need to understand the issue.

user435421
  • 859
  • 2
  • 13
  • 31
  • Use `BigDecimal` instead. `double`s are inherently imprecise. – Federico klez Culloca Dec 07 '20 at 09:29
  • 64bit double does not have the range for that, they can be used basically up to 15 digits. You need another datatype if you want to have such big precision. – Sami Kuhmonen Dec 07 '20 at 09:32
  • You can't. `double` does not have enough precision to represent the number you assigned. – Sweeper Dec 07 '20 at 09:32
  • Java Double is a floating point type with a limited precision. If you have too many significant digits you will loose some information and Double starts rounding. Use BigDecimal for better precision. Or if you just transport values, without doing math, consider using String instead of Double. – Axel Podehl Dec 07 '20 at 09:32
  • You can't avoid this, and indeed this would be a source of bugs if you were relying on it being precisely represented. Such a floating point literal is flagged as a problem [in Google's error prone](http://errorprone.info/bugpattern/FloatingPointLiteralPrecision), for example. – Andy Turner Dec 07 '20 at 09:57
  • After your edit, we had [a similar thread](https://stackoverflow.com/questions/65146241/how-to-check-if-float-is-being-truncated-by-float-parsefloat-from-string) last week. Check it out. – Federico klez Culloca Dec 07 '20 at 10:00
  • You can easily convert a double to a String without problems or "losing digits." The problem is that you've already lost the precision by converting it from a String to a double. – NomadMaker Dec 07 '20 at 10:34

1 Answers1

1

You are using the wrong datatype. With a double you can‘t store that much numbers after the decimal separator. You should go for a BigDecimal edit:

String number = "12113411122222.545421";
BigDecimal value = new  BigDecimal(number);
System.out.println(String.format("%f", value));

I hope I could help you.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
IX0Y3
  • 123
  • 1
  • 1
  • 8