1. BigInteger类的常用方法
方法 |
含义 |
public BigInteger(String val) |
将 BigInteger 的十进制字符串表示形式转换为 BigInteger。 |
public BigInteger add(BigInteger val) |
返回其值为 (this + val ) 的 BigInteger 。 |
public BigInteger subtract(BigInteger val) |
返回其值为 (this - val ) 的 BigInteger 。 |
public BigInteger multiply(BigInteger val) |
返回其值为 (this * val ) 的 BigInteger 。 |
public BigInteger divide(BigInteger val) |
返回其值为 (this / val ) 的 BigInteger 。 |
package date_3_23;
import org.junit.Test;
import java.math.BigInteger;
public class BigIntegerTest {
/**
* 测试 int 和 long 的取值范围
*/
@Test
public void testIntLongLimit() {
int intMaxValue = Integer.MAX_VALUE;
System.out.println("int表示的最大范围" + intMaxValue); // 2147483647
long longMaxValue = Long.MAX_VALUE;
System.out.println("long表示的最大范围" + longMaxValue); // 9223372036854775807
}
/**
* BigInteger 构造方法的使用
*/
@Test
public void testBigIntegerConstructor() {
BigInteger bigInteger = new BigInteger("92233720368547758079223372036854775807");
String data = bigInteger.toString();
System.out.println(data); // 92233720368547758079223372036854775807
System.out.printf("%s",bigInteger); // 92233720368547758079223372036854775807
}
/**
* 使用 BigInteger 完成两个超大整数的加法运算
*/
@Test
public void testBigIntegerAdd() {
BigInteger left = new BigInteger("92233720368547758079223372036854775807");
BigInteger right = new BigInteger("10000000000000000000000000000000000000");
BigInteger addResult = left.add(right);
System.out.printf("%s + %s = %s\n", left, right, addResult);
// 92233720368547758079223372036854775807 + 10000000000000000000000000000000000000 = 102233720368547758079223372036854775807
}
/**
* 使用 BigInteger 完成两个超大整数的减法运算
*/
@Test
public void testBigIntegerSubtract() {
BigInteger left = new BigInteger("92233720368547758079223372036854775807");
BigInteger right = new BigInteger("10000000000000000000000000000000000000");
BigInteger addResult = left.subtract(right);
System.out.println(addResult);
// 82233720368547758079223372036854775807
}
/**
* 使用 BigInteger 完成两个超大整数的乘法运算
*/
@Test
public void testBigIntegerMultiply() {
BigInteger left = new BigInteger("92233720368547758079223372036854775807");
BigInteger right = new BigInteger("10000000000000000000000000000000000000");
BigInteger addResult = left.multiply(right);
System.out.println(addResult);
//922337203685477580792233720368547758070000000000000000000000000000000000000
}
/**
* 使用 BigInteger 完成两个超大整数的除法运算
*/
@Test
public void testBigIntegerDivide() {
BigInteger left = new BigInteger("92233720368547758079223372036854775807");
BigInteger right = new BigInteger("10000000000000000000000000000000000000");
BigInteger addResult = left.divide(right);
System.out.println(addResult); // 9
}
}
-
BigInteger
没有对应的基本数据类型,但是它提供了方法来转换对应的基本数据类型。
public class BigIntegerTest {
/**
* BigInteger 的类型转化
*/
@Test
public void testBigIntegerTypeConversion() {
BigInteger left = new BigInteger("92233720368547758079223372036854775807");
BigInteger right = new BigInteger("10000000000000000000000000000000000000");
BigInteger divideResult = left.divide(right);
System.out.println(divideResult); // 9
int intValue = divideResult.intValue();
System.out.println("intValue = " + intValue);
long longValue = divideResult.longValue();
System.out.println("longValue = " + longValue);
}
}
2. BigDecimal 类常用方法的使用
- 浮点数在计算的时候可能有精度问题的,在日常开发中,如果有浮点数的运算,禁止使用
float
或者 double
,这是由于浮点数在内存中的存储机制导致的。
/**
* 浮点数的精度问题
*/
@Test
public void testFloatPrecision(){
System.out.println(1.0 - 0.1); // 0.9
System.out.println(1.0 - 0.32); // 0.6799999999999999
}
方法 |
含义 |
public BigDecimal(String val) |
将 BigDecimal 的字符串表示形式转换为 BigDecimal 。 |
public BigDecimal add(BigDecimal augend) |
返回一个 BigDecimal ,其值为 (this + augend )。 |
public BigDecimal subtract(BigDecimal subtrahend) |
返回一个 BigDecimal ,其值为 (this - subtrahend )。 |
public BigDecimal multiply(BigDecimal multiplicand) |
返回一个 BigDecimal ,其值为 (this × multiplicand ) |
public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) |
返回一个 BigDecimal ,其值为 (this / divisor ),其标度为指定标度。如果必须执行舍入,以生成具有指定标度的结果,则应用指定的舍入模式 |
import org.junit.Test;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class BigDecimalTest {
/**
* 浮点数的精度问题
*/
@Test
public void testFloatPrecision(){
System.out.println("1.0 - 0.1 = " + (1.0-0.1)); // 1.0 - 0.1 = 0.9
System.out.println("1.0 - 0.32 = "+ (1.0-0.32));
// 1.0 - 0.32 = 0.6799999999999999
}
/**
* !!! 错误的构造方法
* 在使用 BigDecimal的构造方法创建 BigDecimal 时千万不能传 double 类型的数据,
* 否则即使使用 BigDecimal 做运算, 一样会有精度问题。
*/
@Test
public void testBigDecimalConstructorDouble(){
BigDecimal left = new BigDecimal(1.0);
BigDecimal right = new BigDecimal(0.32);
BigDecimal subtractResult = left.subtract(right);
System.out.printf("%s - %s = %s",left,right,subtractResult);
// 1 - 0.320000000000000006661338147750939242541790008544921875 = 0.679999999999999993338661852249060757458209991455078125
}
/**
* 正确的构造方法!!!
* 应该使用 String 类型的数据 作为构造方法的参数,这样运算就不会有精度问题
*/
@Test
public void testBigDecimalConstructorString(){
BigDecimal left = new BigDecimal("1.0");
BigDecimal right = new BigDecimal("0.32");
BigDecimal subtractResult = left.subtract(right);
System.out.printf("%s - %s = %s",left,right,subtractResult);
// 1.0 - 0.32 = 0.68
}
/**
* BigDecimal 的除法运算
*/
@Test
public void testBigDecimalDivide(){
BigDecimal left = new BigDecimal("10.0");
BigDecimal right = new BigDecimal("3.0");
/**
* BigDecimal divideResult = left.divide(right);
* 当除不尽的时候会发生算数异常 ArithmeticException
*/
/**
* scale 2 表示小数点后保留2位
* RoundingMode.HALF_UP 表示四舍五入
*/
BigDecimal divideResult = left.divide(right, 20,RoundingMode.HALF_UP);
System.out.printf("%s / %s = %s",left,right,divideResult);
}
}