Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse double on string giving wrong result

In my project I am getting a string values from an api and need to pass double values to another api. When I try to parse from string values to double I am not getting the original data.

Here is the code.

String l1="11352721345377306";
String l2="11352721346734307";
String l3="11352721346734308";
String l4="11352721346734309";

DecimalFormat df = new DecimalFormat(".00");

System.out.println(df.format(Double.parseDouble(l1)));
System.out.println(df.format(Double.parseDouble(l2)));
System.out.println(df.format(Double.parseDouble(l3)));  
System.out.println(df.format(Double.parseDouble(l4)));

The output is

11352721345377306.00
11352721346734308.00
11352721346734308.00
11352721346734308.00

What went wrong? Is there any problem with parsing? How can i get the original values back.?

Edit: Without using Decimal Format:

1.1352721345377306E16
1.1352721346734308E16
1.1352721346734308E16
1.1352721346734308E16
like image 825
Madhukar Hebbar Avatar asked Dec 29 '15 12:12

Madhukar Hebbar


1 Answers

You can't get original values back. Refer this Java's Floating-Point (Im)Precision.

like image 141
v.ladynev Avatar answered Oct 03 '22 03:10

v.ladynev