一.大数值运算类
1.整数类型的大数值运算类 - BigInteger
代码实现:
package com. dream. big ;
import java. math. BigInteger ;
public class Test01 {
public static void main ( String [ ] args) {
BigInteger big1 = new BigInteger ( "123456789123456789123456789" ) ;
BigInteger big2 = new BigInteger ( "123456789123456789123456789" ) ;
BigInteger add = big1. add ( big2) ;
System . out. println ( add) ;
BigInteger subtract = big1. subtract ( big2) ;
System . out. println ( subtract) ;
BigInteger multiply = big1. multiply ( big2) ;
System . out. println ( multiply) ;
BigInteger divide = big1. divide ( big2) ;
System . out. println ( divide) ;
}
}
2.小数类型的大数值运算类 - BigDecimal
代码实现:
package com. dream. big ;
import java. math. BigDecimal ;
public class Test02 {
public static void main ( String [ ] args) {
BigDecimal big1 = new BigDecimal ( "0.5" ) ;
BigDecimal big2 = new BigDecimal ( "0.4" ) ;
BigDecimal add = big1. add ( big2) ;
System . out. println ( add) ;
BigDecimal subtract = big1. subtract ( big2) ;
System . out. println ( subtract) ;
BigDecimal multiply = big1. multiply ( big2) ;
System . out. println ( multiply) ;
BigDecimal divide = big1. divide ( big2) ;
System . out. println ( divide) ;
}
}
注意:
import java. math. BigDecimal ;
public class Test03 {
public static void main ( String [ ] args) {
BigDecimal big1 = new BigDecimal ( "10" ) ;
BigDecimal big2 = new BigDecimal ( "3" ) ;
BigDecimal divide = big1. divide ( big2, 2 , BigDecimal . ROUND_HALF_UP) ;
System . out. println ( divide) ;
}
}