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