java BigInteger类
BigInteger功能
可以对大整数进行运算
BigInteger加减乘除
构造方法:
public BigInter(String value)
//可以将整数的字符串,转化为BigInteger对象
成员方法:
public BigInter add(BigInter value) 超大整数加法运算
public BigInter subtract(BigInter value) 超大整数减法运算
public BigInter multiply(BigInter value) 超大整数乘法运算
public BigInter divide(BigInter value) 超大整数除法运算
使用方法
public class BigIntegerDemo {
public static void main(String[] args) {
BigInteger bigInteger = new BigInteger("200");
BigInteger result1 = bigInteger.add(new BigInteger("100"));
System.out.println(result1);
//200 = 200 + 100;
BigInteger result2 = bigInteger.subtract(new BigInteger("50"));
System.out.println(result2);
//150 = 200 - 50;
BigInteger result3 = bigInteger.multiply(new BigInteger("2"));
System.out.println(result3);
//400 = 200 * 2;
BigInteger result4 = bigInteger.divide(new BigInteger("2"));
System.out.println(result4);
//100 = 200 / 2;
}
}
java BigDecimal类
使用场景
BigDecimal可以对大浮点数进行运算,保证运算的精确性。float,double
他们在存储运算的时候,会导致数据精度的丢失。如果要保证运算的准确性,
就需要使用BigDecimal.
BigDecimal加减乘除
构造方法:
public BigDecimal(String val) 将BigDecimal的字符串表示形式转化为 BigDecimal
成员方法:
public BigDecimal add(BigDecimal value) 加法运算
public BigDecimal subtract(BigDecimal value) 减法运算
public BigDecimal multiply(BigDecimal value) 乘法运算
public BigDecimal divide(BigDecimal value) 除法运算(除不尽会有异常)
public BigDecimal divide(BigDecimal divisor , int scale ,int roundingMode) 除法运算(除不使用该方法)
参数说明:
scale 精确位数
roundingMode 取舍模式
BigDecimal.ROUND_HALF_UP 四舍五入
BigDecimal.ROUND_FLOOR 去尾法
BigDecimal.ROUND_UP 进一法
使用方法
public class BigDecimalDemo {
public static void main(String[] args) {
BigDecimal bigDecimal1 = new BigDecimal("10");
BigDecimal bigDecimal2 = new BigDecimal("3");
BigDecimal add = bigDecimal1.add(bigDecimal2);
System.out.println(add);
//13
BigDecimal subtract = bigDecimal1.subtract(bigDecimal2);
System.out.println(subtract);
//7
BigDecimal multiply = bigDecimal1.multiply(bigDecimal2);
System.out.println(multiply);
//30
BigDecimal divide = bigDecimal1.divide(bigDecimal2, 3, BigDecimal.ROUND_FLOOR);
System.out.println(divide);
//3.333
}
}
学的不是技术,更是梦想!!!