轉換的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,將會這樣做。 –