JAVA的两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类,理论上能够表示无限大的数。
1:大整数:BigInteger
-
import java.util.*;
-
import java.math.*;
-
-
public class Test {
-
public static void main(String[] args) {
-
Scanner cin = new Scanner(System.in);
-
-
-
BigInteger A = BigInteger.ONE;
-
System.out.println("BigInteger.ONE的结果为 " + A);
-
BigInteger B = BigInteger.TEN;
-
System.out.println("BigInteger.TEN的结果为 " + B);
-
BigInteger C = BigInteger.ZERO;
-
System.out.println("BigInteger.ZERO的结果为 " + C);
-
-
-
BigInteger c = new BigInteger("12345670",8);
-
System.out.println(c);
-
BigInteger d = BigInteger.valueOf(100);
-
BigInteger e = new BigInteger(new byte[]{1,0});
-
System.out.println(e);
-
System.out.println(e.bitCount());
-
System.out.println(e.bitLength());
-
-
-
System.out.println("请输入大整数a,b");
-
while (cin.hasNext()) {
-
BigInteger a = cin.nextBigInteger();
-
BigInteger b = cin.nextBigInteger();
-
BigInteger c1 = a.add(b);
-
System.out.println("加的结果为 " + c1);
-
BigInteger c2 = a.subtract(b);
-
System.out.println("减的结果为 " + c2);
-
BigInteger c3 = a.multiply(b);
-
System.out.println("乘的结果为 " + c3);
-
BigInteger c4 = a.divide(b);
-
System.out.println("除的结果为 " + c4);
-
BigInteger c5 = a.mod(b);
-
System.out.println("模的结果为 " + c5);
-
BigInteger cc5 = a.remainder(b);
-
System.out.println("余的结果为 " + cc5);
-
BigInteger c6 = a.max(b);
-
System.out.println("最大为 " + c6);
-
BigInteger c7 = a.min(b);
-
System.out.println("最小为 " + c7);
-
BigInteger c8 = a.pow(10);
-
System.out.println("指数运算结果为" + c8);
-
if (a.equals(b))
-
System.out.println("相等");
-
else
-
System.out.println("不相等");
-
BigInteger c10 = a.abs();
-
System.out.println("a的绝对值为 " + c10);
-
BigInteger c11 = a.negate();
-
System.out.println("a的相反数为 " + c11);
-
}
-
}
-
}
-
-
-
2:大浮点数:
import java.util.*;
import java.math.*;
public class Test {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
BigDecimal A = BigDecimal.ONE;
System.out.println("BigDecimal.ONE的结果为 " + A);
BigDecimal B = BigDecimal.TEN;
System.out.println("BigDecimal.TEN的结果为 " + B);
BigDecimal C = BigDecimal.ZERO;
System.out.println("BigDecimal.ZERO的结果为 " + C);
BigDecimal c = new BigDecimal("89.1234567890123456789");
BigDecimal d = new BigDecimal(100);
BigDecimal e = new BigDecimal(new char[]{‘2‘,‘1‘,‘.‘,‘2‘});
System.out.println(e);
System.out.println("请输入大整数a,b");
while (cin.hasNext()) {
BigDecimal a = cin.nextBigDecimal();
BigDecimal b = cin.nextBigDecimal();
BigDecimal c1 = a.add(b);
System.out.println("加的结果为 " + c1);
BigDecimal c2 = a.subtract(b);
System.out.println("减的结果为 " + c2);
BigDecimal c3 = a.multiply(b);
System.out.println("乘的结果为 " + c3);
BigDecimal c4 = a.divide(b);
System.out.println("除的结果为 " + c4);
BigDecimal cc5 = a.remainder(b);
System.out.println("余的结果为 " + cc5);
BigDecimal c6 = a.max(b);
System.out.println("最大为 " + c6);
BigDecimal c7 = a.min(b);
System.out.println("最小为 " + c7);
BigDecimal c8 = a.pow(10);
System.out.println("指数运算结果为" + c8);
if (a.equals(b))
System.out.println("相等");
else
System.out.println("不相等");
BigDecimal c10 = a.abs();
System.out.println("a的绝对值为 " + c10);
BigDecimal c11 = a.negate();
System.out.println("a的相反数为 " + c11);
}
}
}
3:常用格式转换:
-
-
BigDecimal bd = new BigDecimal("12000.87300");
-
bd = bd.stripTrailingZeros();
-
System.out.println(bd);
-
bd = new BigDecimal("1.2E-3");
-
-
-
if(bd.scale()<0){
-
bd = bd.setScale(0);
-
}
-
System.out.println(bd);
-
-
bd = new BigDecimal("12000.873000");
-
bd = bd.setScale(5, BigDecimal.ROUND_HALF_UP);
-
System.out.println(bd);
Java基础之BigInteger,BigDecimal
原文:http://blog.csdn.net/u010552723/article/details/44968085