Wednesday, July 13, 2016

Double precision issue



Problem


I want to get double value same as input that we have given, but unexpectedly the output was:
 
Required value is : 9.999E7 

public class DoubleDemo {
 
   public static void main(String[] args) {
 
      double value = 99999999.9;
      System.out.println("Required value is : " + value);
   }
}

Solution

 
public class DoubleDemo {
 
   public static void main(String[] args) {
 
      double value = 99999999.9;
      System.out.println("Required value is : " + BigDecimal.valueOf(value));
   }
}
 
Here we can get exact output ,

Output is : 99999999.9

No comments:

Post a Comment