Java Big Decimal

A little summary about Big Decimal

BigDecimal is one of Java's APIs Since ordinary numeric types are processed in binary, unintended numbers may be returned. However, by using BigDecimal, it can be treated as a decimal number.

Number.java


import java.math.BigDecimal;
import java.math.RoundingMode;

public class Number {
  public static void main(String[] args) {
    BigDecimal number1 = new BigDecimal("0.2");     //The argument is""Surround with
    BigDecimal number2 = new BigDecimal("4");
    System.out.println(number1);
          
    System.out.println(BigDecimal.ZERO);    //0
    System.out.println(BigDecimal.ONE);     //1
    System.out.println(BigDecimal.TEN);     //10

    System.out.println(number1.add(number2));         
    //Addition number1+ number2
    System.out.println(number1.subtract(number2));  
    //Subtraction number1- number2
    System.out.println(number1.multiply(number2));  
    //Multiply number1* number2
    System.out.println(number1.divide(number2, 3, RoundingMode.UP));  
    //Division number1/ number2,Display up to 3 decimal places,Round up
    //RoundingMode.Is rounded up, rounded down, rounded off, etc.

    BigDecimal number3 = new BigDecimal("0.22");
    BigDecimal value1 = number3.scaleByPowerOfTen(2);  //10 squared
    System.out.println(value1);  //22

    BigDecimal value2 = number3.scaleByPowerOfTen(-2);  //10-Square
    System.out.println(value2);  //0.0022

    BigDecimal value3 = number3.negate();  //Negative
    System.out.println(value3);   //-0.22
     

  }
}

that's all. There are still many functions, so I would like to try it.

Recommended Posts

Java Big Decimal
[Java] Use Big Decimal properly ~ 2018 ~
[Ruby] Big Decimal and DECIMAL
Big Decimal in Kotlin
[java] Decimal number → n-ary number n-ary number → decimal number
How to use Big Decimal
Java
Java
[Note] Handling of Java decimal point
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java string
java (array)
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
java (encapsulation)
[Java] Tips and error issues when converting from double to Big Decimal
Java inheritance
[Java] Overload
Java basics
Decompile Java
[Java] Annotation
java notes
java beginner