转换的Integer
到BigInteger
比划分两个BigInteger
,如下:
BigInteger b = BigInteger.valueOf(10);
int x = 6;
//convert the integer to BigInteger.
BigInteger converted = new BigInteger(Integer.toString(x));
//now you can divide, add, subtract etc.
BigInteger result = b.divide(converted); //but this will give you Integer values.
System.out.println(result);
result = b.add(converted);
System.out.println(result);
师以上会给你区划Integer
值,得到精确值,使用BigDecimal
。
编辑:
要删除两个中间变量converted
和在上面的代码result
:
BigInteger b = BigInteger.valueOf(10);
int x = 6;
System.out.println(b.divide(new BigInteger(Integer.toString(x))));
OR
Scanner in = new Scanner(System.in);
System.out.println(BigInteger.valueOf((in.nextInt())).divide(new BigInteger(Integer.toString(in.nextInt()))));
请阅读[Javadoc中'BigInteger'](https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html) –
转换整型为BigInteger ,然后使用采用BigInteger参数的各种方法 – yshavit
感谢Jim和yshavit,将会这样做。 –