Facing issue while converting 18 digit double value to long

61 Views Asked by At

I am receiving double value [18-19 digits] as input to my method and I have to convert it to long value. I have no interest in handling the decimal part of input as input will always be ending with .0

For example ,
input will be like

double val1 = 1000000000000007403.0;
double val2 = 2223372036854774845.0;

I tried below code,

In below code example, my input is val1 and val2.

public static void main (String args[]){
        
    double val1 = 1000000000000007403.0;   // input
    double val2 = 2223372036854774845.0;   // input

    Double double1 = Double.valueOf(val1);
    Double double2 = Double.valueOf(val2);
        
    long val1Long = double1.longValue();
    long val2Long = double2.longValue();
    System.out.println(val1Long);
    System.out.println(val2Long);   
}

But it is returning wrong output.

Output:

1000000000000007424
2223372036854774784

Expected output:

1000000000000007403
2223372036854774845

I am attaching the debug screenshot of method as well. enter image description here I am facing this issue whenever my double value is greater than 14-15 digits.

0

There are 0 best solutions below