简体   繁体   中英

Java double converts more than 6 decimal points to exponential format

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM