0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

BigDecimal

Posted at
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)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?