I have an app which is reading data from PostGresql DB. The column is defined as real
. In the java code I am reading the value of this column as BigDecimal
. What is happening, the value of this column in DB is 0.18
. When I read this data, the java output is showing as 0.180000007
. If I define the column as Float
in java, I am getting the value as 0.18
. I am not doing any monetary calculations in my app. Can someone explain me why BigDecimal is showing so many digits after decimal?
Please refer to the code below, I am trying to understand why BigDecimal is giving so many digits after the decimal:
public class BigDecimalTest {
public static void main(String[] args){
float float_val = 0.18f;
BigDecimal output = new BigDecimal(float_val);
System.out.println("BigDecimal output is: "+ output);
System.out.println("Float output is: "+float_val);
}
}
Output:
BigDecimal output is: 0.180000007152557373046875
Float output is: 0.18