0.7 + 0.1 = 0.8, but if you let the computer calculate it, an error will occur. I will omit the reason why it comes out (** see below **). https://www.php.net/manual/ja/language.types.float.php
The reference book uses ** Big Decimal ** to deal with it.
demojava/demo5/Demo5.java
package demojava.demo5;
import java.math.BigDecimal;
public class Demo5 {
public static void main(String [] args) {
double test1 = 0.0;
test1 = 0.7 + 0.1;
System.out.println("test1 = " + test1);
test2();
}
public static void test2() {
BigDecimal val1 = new BigDecimal("0.7");
BigDecimal val2 = new BigDecimal("0.1");
BigDecimal val = val1.add(val2);
System.out.println("test2 = " + val);
}
}
Recommended Posts