0

I have a simple task of dividing 1 by 10 power n where n value is dynamic. I need to save the result in a double variable. Until the divisor's value is 1000000 the double variable shows it properly which is 0.000001. Once the divisor value is more than 6 zeroes the value saved in double is in exponential form (scientific format ex: 1E-8). How do I resolve this problem as the double variable gets sent back as 0 when converted to json.

Things I cannot do: 1) Cannot use string as the variable is defined as double in an external Jar I use. I cannot change the type of the field. Most formatters use String. 2) I have no control on how many digits the divisor would be. It may be more than 15 digits too in some cases. 3) All the code that handles the conversion from object is done by jackson so I do not have control over there.

Gassa
  • 8,546
  • 3
  • 29
  • 49
Kartheek Mannepalli
  • 186
  • 1
  • 2
  • 15
  • Can you store `n` instead of `1/10^n` in the JSON, and handle exponentiation elsewhere? – Gassa Feb 16 '16 at 16:41
  • Possible duplicate: http://stackoverflow.com/q/7299576/1488799 – Gassa Feb 16 '16 at 16:44
  • A double variable cannot hold different string representations, it is always IEEE 64-bit floating point, so something else is choosing to represent your double as a string in scientific notation. You mention that the variable gets sent back to 0 when converted to json - can you verify that the value is 0 in the json, or is it 1E-8 in the json and gets rounded to 0 when loading back into a double variable? This would help identify where it is getting rounded to 0. – Matt Jordan Feb 16 '16 at 16:46
  • @Gassa I cannot use n in the JSON as I am trying to create a json schema dynamically and it requires a value similar to 0.000001. Thank you for the link I will read through that. – Kartheek Mannepalli Feb 16 '16 at 16:50
  • @MattJordan The value is getting rounded of to 0 because I see the value being 1E-8 before its gets converted to Json. I tried string but that shows the value as 1E-8 which would not work me in the Json Schema. Is there a way out of it other than changing the Jackson module to format it which I am not sure is possible. – Kartheek Mannepalli Feb 16 '16 at 16:53
  • 1E-8 is IMHO a perfectly valid number in JSON - you should rather post a snippet of code on how you convert your double to JSON... – Gyro Gearless Feb 16 '16 at 17:12

1 Answers1

1

use

double mydouble = 12345678;
System.out.printf("mydouble: %f\n", mydouble);

this would print out

    mydouble: 12345678.000000

taken from :- How to print double value without scientific notation using Java?

Community
  • 1
  • 1