public class SampleProject {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
BigInteger bi = BigInteger.valueOf(1);
System.out.println(bi);
BigDecimal bd = BigDecimal.valueOf(100);
BigDecimal bd2 = bd.multiply(BigDecimal.valueOf(3));
System.out.println(bd2.longValue());
bd2 = bd.divide(BigDecimal.valueOf(5));
System.out.println(bd2.longValue());
bd2 = BigDecimal.valueOf(100);
bd2 = bd2.divide(BigDecimal.valueOf(3),3,RoundingMode.HALF_UP);
System.out.println(bd2.doubleValue());
bd2 = BigDecimal.valueOf(100);
bd2 = bd2.divide(BigDecimal.valueOf(3)); //循環小数でエラーになる
}
1
300
20
33.333
Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
at java.base/java.math.BigDecimal.divide(BigDecimal.java:1736)
at sampleproject.SampleProject.main(SampleProject.java:32)
C:\Users\java\AppData\Local\NetBeans\Cache\15\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\java\AppData\Local\NetBeans\Cache\15\executor-snippets\run.xml:68: Java returned: 1
BUILD FAILED (total time: 0 seconds)