回答編集履歴
1
タイプミスの修正とエラーの時の説明を追記
answer
CHANGED
@@ -18,8 +18,15 @@
|
|
18
18
|
0.78
|
19
19
|
```
|
20
20
|
計算結果は 0.7777・・・の循環小数なので、小数第3位を四捨五入させた値になりました。
|
21
|
-
ここで、もし小数点以下の桁数を指定しないと、無限の桁数が必要になってしまって BigDecimal の divide メソッドは
|
21
|
+
ここで、もし小数点以下の桁数を指定しないと、無限の桁数が必要になってしまって BigDecimal の divide メソッドは ArithmeticException をスローするので注意してください。
|
22
22
|
|
23
|
+
BigDecimal.valueOf(num2).divide(BigDecimal.valueOf(num1).pow(2)) の場合のエラー
|
24
|
+
```
|
25
|
+
Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
|
26
|
+
at java.base/java.math.BigDecimal.divide(BigDecimal.java:1722)
|
27
|
+
at Main.main(Main.java:22)
|
28
|
+
```
|
29
|
+
|
23
30
|
ご質問にあるコードの場合、上の System.out.println の引数に指定したように toPlainString の結果をsetAttributeすれば良いでしょう。
|
24
31
|
|
25
32
|
```java
|