ACM与Java -- 大整数类的常用函数一览表

BigInteger abs() 
此方法返回一个BigInteger,其值是此BigInteger的绝对值。
2BigInteger add(BigInteger val)
此方法返回一个BigInteger,其值是(this + val).
3BigInteger and(BigInteger val)
此方法返回一个BigInteger,其值是 (this & val).
4BigInteger andNot(BigInteger val) 
此方法返回一个BigInteger,其值是 (this & ~val).
5int bitCount()
此方法返回此BigInteger的二进制补码表示的位,从符号位不同的数字。
6int bitLength() 
此方法返回位在此BigInteger的最小的二进制补码表示的数,不包括符号位。
7BigInteger clearBit(int n)
此方法返回一个BigInteger,其值相当于此BigInteger与指定位清零。
8int compareTo(BigInteger val)
此方法比较此BigInteger与指定的BigInteger。
9BigInteger divide(BigInteger val)
此方法返回一个BigInteger,其值是 (this / val).
10BigInteger[ ] divideAndRemainder(BigInteger val)
此方法返回一个包含两个BigIntegers:(this / val) 和 (this % val),其次是一个数组。
11double doubleValue()
此方法此BigInteger转换为双精度double。 
12boolean equals(Object x)
此方法比较此BigInteger与指定对象是否相等。
13BigInteger flipBit(int n)
此方法返回一个BigInteger,其值相当于此BigInteger与指定位翻转。
14float floatValue()
此方法将BigInteger转换为float。
15BigInteger gcd(BigInteger val) 
此方法返回一个BigInteger,其值是绝对值的最大公约数:abs(this) 和abs(val)。
16int getLowestSetBit()
此方法返回最右边的(最低阶)的索引在此BigInteger1比特(零比特的数量,以最右侧的1位的右侧)。
17int hashCode()
此方法返回此BigInteger的哈希代码。
18int intValue()
此方法此BigInteger转换为int。
19boolean isProbablePrime(int certainty)
此方法返回true,如果此BigInteger是素数,其绝对复合数则返回false。
20long longValue()
些方法将BigInteger转换为long。
21BigInteger max(BigInteger val)
此方法返回此BigInteger和val的最大值。
22BigInteger min(BigInteger val)
此方法返回此BigInteger和val的最小值。
23BigInteger mod(BigInteger m)
此方法返回一个BigInteger,其值是(this mod m).
24BigInteger modInverse(BigInteger m)
此方法返回一个BigInteger,其值是 (this-1 mod m).
25BigInteger modPow(BigInteger exponent, BigInteger m)
此方法返回一个BigInteger,其值是 (thisexponent mod m).
26BigInteger multiply(BigInteger val)
此方法返回一个BigInteger,其值是 (this * val).
27BigInteger negate()
此方法返回一个BigInteger,其值是 (-this).
28BigInteger nextProbablePrime() 
此方法返回一个整数大于该BigInteger的可能是素数。
29BigInteger not()
此方法返回一个BigInteger,其值是 (~this).
30BigInteger or(BigInteger val)
此方法返回一个BigInteger,其值是 (this | val).
31BigInteger pow(int exponent)
此方法返回一个BigInteger,其值是(thisexponent).
32static BigInteger probablePrime(int bitLength, Random rnd)
此方法返回一个正BigInteger的可能是素数,以指定的bitLength。
33BigInteger remainder(BigInteger val)
此方法返回一个BigInteger,其值是 (this % val).
34BigInteger setBit(int n)
此方法返回一个BigInteger,其值相当于此BigInteger与指定的位设置。
35BigInteger shiftLeft(int n)
此方法返回一个BigInteger,其值是 (this << n).
36BigInteger shiftRight(int n)
此方法返回一个BigInteger,其值是 (this >> n).
37int signum() 
This method returns the signum function of this BigInteger.
38BigInteger subtract(BigInteger val)
此方法返回一个BigInteger,其值是 (this - val).
39boolean testBit(int n)
此方法返回当且仅当所指定的位被设置为真。
40byte[ ] toByteArray()
此方法返回一个包含此BigInteger的二进制补码表示的字节数组。
41String toString() 
此方法返回此BigInteger的十进制字符串表示形式。
42String toString(int radix) 
此方法返回在给定的基数以BigInteger的字符串表示形式。
43static BigInteger valueOf(long val)
此方法返回一个BigInteger,其值等于指定long。
44BigInteger xor(BigInteger val) 
此方法返回一个BigInteger,其值是 (this ^ val).

详情https://www.yiibai.com/java/math/java_math_bigdecimal.html

大整数类: 

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;
 
public class q {
 
	public static void main(String[] args) {

		BigInteger a = BigInteger.valueOf(20);
		BigInteger b = BigInteger.valueOf(450);
		System.out.println(a);
		System.out.println(b);
	
		//加法
		System.out.println(a.add(b));
		System.out.println(a.add(a));
		System.out.println(a.add(b).add(b));
		
		//减法
		System.out.println(a.subtract(a));
		System.out.println(a.subtract(b));
		System.out.println(b.subtract(a).subtract(a));
		
		//乘法
		System.out.println(a.multiply(a));
		System.out.println(a.multiply(b));
		System.out.println(a.multiply(b).multiply(b));
		
		//除法
		System.out.println(a.divide(a));
		System.out.println(a.divide(b));//10/2000=0
		System.out.println(b.divide(a));
		System.out.println(b.divide(a).divide(a));
		
		//取模
		BigInteger mod = BigInteger.valueOf(3);
		System.out.println(b.mod(mod));
	}
}

大数:

构造器描述 

BigDecimal(int)       创建一个具有参数所指定整数值的对象。 

BigDecimal(double) 创建一个具有参数所指定双精度值的对象。 

BigDecimal(long)    创建一个具有参数所指定长整数值的对象。 

BigDecimal(String) 创建一个具有参数所指定以字符串表示的数值的对象。

方法描述 

add(BigDecimal)        BigDecimal对象中的值相加,然后返回这个对象。 

subtract(BigDecimal) BigDecimal对象中的值相减,然后返回这个对象。 

multiply(BigDecimal)  BigDecimal对象中的值相乘,然后返回这个对象。 

divide(BigDecimal)     BigDecimal对象中的值相除,然后返回这个对象。 

toString()                将BigDecimal对象的数值转换成字符串。  (一般都用toPlainString())

doubleValue()          将BigDecimal对象中的值以双精度数返回。 

floatValue()             将BigDecimal对象中的值以单精度数返回。 

longValue()             将BigDecimal对象中的值以长整数返回。 

intValue()               将BigDecimal对象中的值以整数返回。 

常用搭配:stripTrailingZeros().toPlainString();  在保证数值不变的前提下,去除后缀0,并且保留十进制的特性。
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;
 
public class q {
 
	public static void main(String[] args) {
        //如果加引号了或者是直接控制台读入的,默认使用了string的构造器,否则相当于是int
		BigDecimal a = new BigDecimal(10);
		BigDecimal b = new BigDecimal(10.5);
		BigDecimal c = new BigDecimal(123456789101222L);
		BigDecimal d = new BigDecimal("-123456.31");
 
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		System.out.println(d);
		
		//四则运算
		System.out.println(a.add(b).add(c).add(d));
		System.out.println(a.subtract(b).subtract(a));
		System.out.println(a.multiply(b).multiply(c));
		System.out.println(c.divide(a).divide(new BigDecimal(20)));
		System.out.println(d.divide(a));
		System.out.println(d.doubleValue());
 
		System.out.println(a.compareTo(a));// 0
		System.out.println(a.compareTo(b));// -1
		System.out.println(b.compareTo(a));// 1
 
		BigDecimal aa = new BigDecimal(10.00);
		BigDecimal bb = new BigDecimal(10.000);
		BigDecimal cc = new BigDecimal(200);
        String aa = di.stripTrailingZeros().toPlainString();
		System.out.println(aa.equals(aa));// true?
		System.out.println(aa.equals(bb));// true?
		System.out.println(aa.equals(cc));//false
	}
 
}

保留位数:

例如:1.将f1保留2位有效数字输出2.将f1保留4位有效数字输出1.System.out.println((double)(Math.round(f1*100)/100.0));2.System.out.println((double)(Math.round(sd3*100)/100.0));注意:若数据只有小数点后第n位为0,则保留到不为0的位;如3.14003保留1位结果:3.1保留2位结果:3.14保留3位结果:3.14保留4位结果:3.14保留5位结果:3.1400
例如:1.将f1保留2位有效数字输出 
2.将f1保留4位有效数字输出 
1.System.out.println((double) (Math.round(f1*100)/100.0)); 

2.System.out.println((double) (Math.round(sd3*100)/100.0)); 

注意:若数据只有小数点后第n位为0,则保留到不为0的位; 
如 3.14003 
保留1位结果:3.1 
保留2位结果:3.14 
保留3位结果:3.14 
保留4位结果:3.14 
保留5位结果:3.14003 



另一种办法: 

import java.text.DecimalFormat; 

DecimalFormat df2 = new DecimalFormat("###.00"); 

DecimalFormat df2 = new DecimalFormat("###.000"); 

System.out.println(df2.format(f1)); 

第一个为2位,第二个为3位. 

如 3.14003 
保留1位结果:3.1 
保留2位结果:3.14 
保留3位结果:3.140 
保留4位结果:3.1400 
保留5位结果:3.14003 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值