Java : BigDecimal (正確な10進数) - API使用例
BigDecimal (Java SE 18 & JDK 18) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
BigDecimalクラスは、10進数としての正確な値を表現するクラスです。
プリミティブ型の double や float では誤差が発生する演算も、BigDecimalを使えば誤差なしで演算できます。
// 誤差が発生する例です。
final double d1 = 0.1;
final double d2 = 0.2;
final double d3 = d1 + d2;
System.out.println(d1); // 0.1
System.out.println(d2); // 0.2
System.out.println(d3); // 0.30000000000000004
// BigDecimalを使う例です。
final BigDecimal d1 = new BigDecimal("0.1");
final BigDecimal d2 = new BigDecimal("0.2");
final BigDecimal d3 = d1.add(d2);
System.out.println(d1); // 0.1
System.out.println(d2); // 0.2
System.out.println(d3); // 0.3
BigDecimalクラスには、3つの大事な要素があります。
- スケール (scale)
- 精度 (precision)
- 丸めかた (RoundingMode)
特にスケールと精度はちょっと分かりにくいので、API仕様書をよく確認することをおすすめします。
また、「小数を誤差なしで計算 (BigDecimal)」 の記事でも解説しているので、そちらもご参照いただけたら幸いです。
フィールド
static final BigDecimal ONE
System.out.println(BigDecimal.ONE); // 1
System.out.println(BigDecimal.ONE.scale()); // 0
static final int ROUND_CEILING
非推奨です。
代わりに RoundingMode をご使用ください。
static final int ROUND_DOWN
非推奨です。
代わりに RoundingMode をご使用ください。
static final int ROUND_FLOOR
非推奨です。
代わりに RoundingMode をご使用ください。
static final int ROUND_HALF_DOWN
非推奨です。
代わりに RoundingMode をご使用ください。
static final int ROUND_HALF_EVEN
非推奨です。
代わりに RoundingMode をご使用ください。
static final int ROUND_HALF_UP
非推奨です。
代わりに RoundingMode をご使用ください。
static final int ROUND_UNNECESSARY
非推奨です。
代わりに RoundingMode をご使用ください。
static final int ROUND_UP
非推奨です。
代わりに RoundingMode をご使用ください。
static final BigDecimal TEN
System.out.println(BigDecimal.TEN); // 10
System.out.println(BigDecimal.TEN.scale()); // 0
static final BigDecimal ZERO
System.out.println(BigDecimal.ZERO); // 0
System.out.println(BigDecimal.ZERO.scale()); // 0
コンストラクタ
BigDecimal (char[] in)
final char[] in = "1234567890".toCharArray();
final var dec = new BigDecimal(in);
System.out.println(dec); // 1234567890
final char[] in = "-1234567890".toCharArray();
final var dec = new BigDecimal(in);
System.out.println(dec); // -1234567890
final char[] in = "0.12345".toCharArray();
final var dec = new BigDecimal(in);
System.out.println(dec); // 0.12345
final char[] in = "xyz".toCharArray();
//new BigDecimal(in); // NumberFormatException:
BigDecimal (char[] in, int offset, int len)
final char[] in = "dummy123dummy".toCharArray();
final var dec = new BigDecimal(in, 5, 3);
System.out.println(dec); // 123
final char[] in = "12345".toCharArray();
System.out.println(new BigDecimal(in, 0, 5)); // 12345
System.out.println(new BigDecimal(in, 0, 4)); // 1234
System.out.println(new BigDecimal(in, 0, 3)); // 123
System.out.println(new BigDecimal(in, 0, 2)); // 12
System.out.println(new BigDecimal(in, 0, 1)); // 1
//new BigDecimal(in, 0, 0); // NumberFormatException: No digits found.
System.out.println(new BigDecimal(in, 0, 5)); // 12345
System.out.println(new BigDecimal(in, 1, 4)); // 2345
System.out.println(new BigDecimal(in, 2, 3)); // 345
System.out.println(new BigDecimal(in, 3, 2)); // 45
System.out.println(new BigDecimal(in, 4, 1)); // 5
BigDecimal (char[] in, int offset, int len, MathContext mc)
offset と len については BigDecimal(char[] in, int offset, int len) の使用例を、mc については BigDecimal(char[] in, MathContext mc) の使用例をご参照ください。
BigDecimal (char[] in, MathContext mc)
final char[] in = "123456789".toCharArray();
final var mcList = List.of(
new MathContext(9),
new MathContext(8),
new MathContext(7),
new MathContext(6));
for (final var mc : mcList) {
final var dec = new BigDecimal(in, mc);
System.out.println("mc ( " + mc + " ) : dec = "
+ dec.toPlainString() + " : scale = " + dec.scale());
}
// 結果
// ↓
//mc ( precision=9 roundingMode=HALF_UP ) : dec = 123456789 : scale = 0
//mc ( precision=8 roundingMode=HALF_UP ) : dec = 123456790 : scale = -1
//mc ( precision=7 roundingMode=HALF_UP ) : dec = 123456800 : scale = -2
//mc ( precision=6 roundingMode=HALF_UP ) : dec = 123457000 : scale = -3
final char[] in = "-123456789".toCharArray();
final var mcList = List.of(
new MathContext(9),
new MathContext(8),
new MathContext(7),
new MathContext(6));
for (final var mc : mcList) {
final var dec = new BigDecimal(in, mc);
System.out.println("mc ( " + mc + " ) : dec = "
+ dec.toPlainString() + " : scale = " + dec.scale());
}
// 結果
// ↓
//mc ( precision=9 roundingMode=HALF_UP ) : dec = -123456789 : scale = 0
//mc ( precision=8 roundingMode=HALF_UP ) : dec = -123456790 : scale = -1
//mc ( precision=7 roundingMode=HALF_UP ) : dec = -123456800 : scale = -2
//mc ( precision=6 roundingMode=HALF_UP ) : dec = -123457000 : scale = -3
final char[] in = "1.2345".toCharArray();
final var mcList = List.of(
new MathContext(5),
new MathContext(4),
new MathContext(3),
new MathContext(2));
for (final var mc : mcList) {
final var dec = new BigDecimal(in, mc);
System.out.println("mc ( " + mc + " ) : dec = "
+ dec.toPlainString() + " : scale = " + dec.scale());
}
// 結果
// ↓
//mc ( precision=5 roundingMode=HALF_UP ) : dec = 1.2345 : scale = 4
//mc ( precision=4 roundingMode=HALF_UP ) : dec = 1.235 : scale = 3
//mc ( precision=3 roundingMode=HALF_UP ) : dec = 1.23 : scale = 2
//mc ( precision=2 roundingMode=HALF_UP ) : dec = 1.2 : scale = 1
BigDecimal (double val)
注意
- このメソッドは思わぬ誤差を発生させることがあります。
API仕様にもありますが、代わりに BigDecimal(String val) を使うことをおすすめします。
// 誤差の発生する例です。
final var dec = new BigDecimal(0.1);
System.out.println(dec); // 0.1000000000000000055511151231257827021181583404541015625
BigDecimal (double val, MathContext mc)
注意
- このメソッドは思わぬ誤差を発生させることがあります。
API仕様にもありますが、代わりに BigDecimal(String val, MathContext mc) を使うことをおすすめします。
final var mc = MathContext.DECIMAL128;
System.out.println(mc); // precision=34 roundingMode=HALF_EVEN
// 誤差の発生する例です。
final var dec = new BigDecimal(0.1, mc);
System.out.println(dec); // 0.1000000000000000055511151231257827
BigDecimal (int val)
final var dec = new BigDecimal(12345);
System.out.println(dec); // 12345
System.out.println(dec.scale()); // 0
final var dec = new BigDecimal(-5678);
System.out.println(dec); // -5678
System.out.println(dec.scale()); // 0
BigDecimal (int val, MathContext mc)
final var mcList = List.of(
new MathContext(5),
new MathContext(4),
new MathContext(3),
new MathContext(2));
for (final var mc : mcList) {
final var dec = new BigDecimal(12345, mc);
System.out.println("mc ( " + mc + " ) : dec = "
+ dec.toPlainString() + " : scale = " + dec.scale());
}
// 結果
// ↓
//mc ( precision=5 roundingMode=HALF_UP ) : dec = 12345 : scale = 0
//mc ( precision=4 roundingMode=HALF_UP ) : dec = 12350 : scale = -1
//mc ( precision=3 roundingMode=HALF_UP ) : dec = 12300 : scale = -2
//mc ( precision=2 roundingMode=HALF_UP ) : dec = 12000 : scale = -3
final var mcList = List.of(
new MathContext(5),
new MathContext(4),
new MathContext(3),
new MathContext(2));
for (final var mc : mcList) {
final var dec = new BigDecimal(-56789, mc);
System.out.println("mc ( " + mc + " ) : dec = "
+ dec.toPlainString() + " : scale = " + dec.scale());
}
// 結果
// ↓
//mc ( precision=5 roundingMode=HALF_UP ) : dec = -56789 : scale = 0
//mc ( precision=4 roundingMode=HALF_UP ) : dec = -56790 : scale = -1
//mc ( precision=3 roundingMode=HALF_UP ) : dec = -56800 : scale = -2
//mc ( precision=2 roundingMode=HALF_UP ) : dec = -57000 : scale = -3
BigDecimal (long val)
型の違い以外は BigDecimal(int val) と同じになります。
そちらのAPI使用例をご参照ください。
BigDecimal (long val, MathContext mc)
型の違い以外は BigDecimal(int val, MathContext mc) と同じになります。
そちらのAPI使用例をご参照ください。
BigDecimal (String val)
final var values = List.of(
"0",
"0.00",
"123",
"-123",
"1.23E3",
"1.23E+3",
"12.3E+7",
"12.0",
"12.3",
"0.00123",
"-1.23E-12",
"1234.5E-4",
"0E+7",
"-0"
);
for (final var val : values) {
final var dec = new BigDecimal(val);
System.out.println("val = " + val + " : dec = "
+ dec.toPlainString() + " : scale = " + dec.scale());
}
// 結果
// ↓
//val = 0 : dec = 0 : scale = 0
//val = 0.00 : dec = 0.00 : scale = 2
//val = 123 : dec = 123 : scale = 0
//val = -123 : dec = -123 : scale = 0
//val = 1.23E3 : dec = 1230 : scale = -1
//val = 1.23E+3 : dec = 1230 : scale = -1
//val = 12.3E+7 : dec = 123000000 : scale = -6
//val = 12.0 : dec = 12.0 : scale = 1
//val = 12.3 : dec = 12.3 : scale = 1
//val = 0.00123 : dec = 0.00123 : scale = 5
//val = -1.23E-12 : dec = -0.00000000000123 : scale = 14
//val = 1234.5E-4 : dec = 0.12345 : scale = 5
//val = 0E+7 : dec = 0 : scale = -7
//val = -0 : dec = 0 : scale = 0
BigDecimal (String val, MathContext mc)
val については BigDecimal(String val) の使用例もご参照ください。
final var mcList = List.of(
new MathContext(4),
new MathContext(3),
new MathContext(2));
for (final var mc : mcList) {
final var dec = new BigDecimal("1234", mc);
System.out.println("mc ( " + mc + " ) : dec = "
+ dec.toPlainString() + " : scale = " + dec.scale());
}
// 結果
// ↓
//mc ( precision=4 roundingMode=HALF_UP ) : dec = 1234 : scale = 0
//mc ( precision=3 roundingMode=HALF_UP ) : dec = 1230 : scale = -1
//mc ( precision=2 roundingMode=HALF_UP ) : dec = 1200 : scale = -2
final var mcList = List.of(
new MathContext(5),
new MathContext(4),
new MathContext(3),
new MathContext(2));
for (final var mc : mcList) {
final var dec = new BigDecimal("1.2345", mc);
System.out.println("mc ( " + mc + " ) : dec = "
+ dec.toPlainString() + " : scale = " + dec.scale());
}
// 結果
// ↓
//mc ( precision=5 roundingMode=HALF_UP ) : dec = 1.2345 : scale = 4
//mc ( precision=4 roundingMode=HALF_UP ) : dec = 1.235 : scale = 3
//mc ( precision=3 roundingMode=HALF_UP ) : dec = 1.23 : scale = 2
//mc ( precision=2 roundingMode=HALF_UP ) : dec = 1.2 : scale = 1
BigDecimal (BigInteger val)
final var val = new BigInteger("12345");
System.out.println(val); // 12345
final var dec = new BigDecimal(val);
System.out.println(dec); // 12345
System.out.println(dec.scale()); // 0
final var val = new BigInteger("-5678");
System.out.println(val); // -5678
final var dec = new BigDecimal(val);
System.out.println(dec); // -5678
System.out.println(dec.scale()); // 0
BigDecimal (BigInteger unscaledVal, int scale)
final var val = new BigInteger("12345");
System.out.println(val); // 12345
final var dec1 = new BigDecimal(val, 4);
System.out.println(dec1); // 1.2345
System.out.println(dec1.scale()); // 4
final var dec2 = new BigDecimal(val, -4);
System.out.println(dec2.toPlainString()); // 123450000
System.out.println(dec2.scale()); // -4
final var val = new BigInteger("-5678");
System.out.println(val); // -5678
final var dec = new BigDecimal(val, 5);
System.out.println(dec); // -0.05678
System.out.println(dec.scale()); // 5
BigDecimal (BigInteger unscaledVal, int scale, MathContext mc)
final var mcList = List.of(
new MathContext(5),
new MathContext(4),
new MathContext(3),
new MathContext(2));
final var val = new BigInteger("12345");
final var scale = 4;
for (final var mc : mcList) {
final var dec = new BigDecimal(val, scale, mc);
System.out.println("mc ( " + mc + " ) : dec = "
+ dec.toPlainString() + " : scale = " + dec.scale());
}
// 結果
// ↓
//mc ( precision=5 roundingMode=HALF_UP ) : dec = 1.2345 : scale = 4
//mc ( precision=4 roundingMode=HALF_UP ) : dec = 1.235 : scale = 3
//mc ( precision=3 roundingMode=HALF_UP ) : dec = 1.23 : scale = 2
//mc ( precision=2 roundingMode=HALF_UP ) : dec = 1.2 : scale = 1
final var mcList = List.of(
new MathContext(5),
new MathContext(4),
new MathContext(3),
new MathContext(2));
final var val = new BigInteger("12345");
final var scale = -4;
for (final var mc : mcList) {
final var dec = new BigDecimal(val, scale, mc);
System.out.println("mc ( " + mc + " ) : dec = "
+ dec.toPlainString() + " : scale = " + dec.scale());
}
// 結果
// ↓
//mc ( precision=5 roundingMode=HALF_UP ) : dec = 123450000 : scale = -4
//mc ( precision=4 roundingMode=HALF_UP ) : dec = 123500000 : scale = -5
//mc ( precision=3 roundingMode=HALF_UP ) : dec = 123000000 : scale = -6
//mc ( precision=2 roundingMode=HALF_UP ) : dec = 120000000 : scale = -7
BigDecimal (BigInteger val, MathContext mc)
final var mcList = List.of(
new MathContext(5),
new MathContext(4),
new MathContext(3),
new MathContext(2));
final var val = new BigInteger("12345");
for (final var mc : mcList) {
final var dec = new BigDecimal(val, mc);
System.out.println("mc ( " + mc + " ) : dec = "
+ dec.toPlainString() + " : scale = " + dec.scale());
}
// 結果
// ↓
//mc ( precision=5 roundingMode=HALF_UP ) : dec = 12345 : scale = 0
//mc ( precision=4 roundingMode=HALF_UP ) : dec = 12350 : scale = -1
//mc ( precision=3 roundingMode=HALF_UP ) : dec = 12300 : scale = -2
//mc ( precision=2 roundingMode=HALF_UP ) : dec = 12000 : scale = -3
メソッド
BigDecimal abs ()
final var dec = new BigDecimal("1234");
System.out.println(dec); // 1234
final var ret = dec.abs();
System.out.println(ret); // 1234
final var dec = new BigDecimal("-5678");
System.out.println(dec); // -5678
final var ret = dec.abs();
System.out.println(ret); // 5678
final var dec = new BigDecimal("-1.2345");
System.out.println(dec); // -1.2345
final var ret = dec.abs();
System.out.println(ret); // 1.2345
BigDecimal abs (MathContext mc)
final var mcList = List.of(
new MathContext(5),
new MathContext(4),
new MathContext(3),
new MathContext(2));
final var dec = new BigDecimal("-12345");
for (final var mc : mcList) {
final var ret = dec.abs(mc);
System.out.println("mc ( " + mc + " ) : ret = "
+ ret.toPlainString() + " : scale = " + ret.scale());
}
// 結果
// ↓
//mc ( precision=5 roundingMode=HALF_UP ) : ret = 12345 : scale = 0
//mc ( precision=4 roundingMode=HALF_UP ) : ret = 12350 : scale = -1
//mc ( precision=3 roundingMode=HALF_UP ) : ret = 12300 : scale = -2
//mc ( precision=2 roundingMode=HALF_UP ) : ret = 12000 : scale = -3
BigDecimal add (BigDecimal augend)
final var dec = new BigDecimal("100");
final var augend = new BigDecimal("23");
final var ret = dec.add(augend);
System.out.println(ret); // 123
final var dec = new BigDecimal("100");
final var augend = new BigDecimal("-23");
final var ret = dec.add(augend);
System.out.println(ret); // 77
final var dec = new BigDecimal("0.1");
final var augend = new BigDecimal("0.023");
final var ret = dec.add(augend);
System.out.println(ret); // 0.123
BigDecimal add (BigDecimal augend, MathContext mc)
final var mcList = List.of(
new MathContext(5),
new MathContext(4),
new MathContext(3),
new MathContext(2));
// 1 + 0.2345
final var dec = new BigDecimal("1");
final var augend = new BigDecimal("0.2345");
for (final var mc : mcList) {
final var ret = dec.add(augend, mc);
System.out.println("mc ( " + mc + " ) : ret = "
+ ret.toPlainString() + " : scale = " + ret.scale());
}
// 結果
// ↓
//mc ( precision=5 roundingMode=HALF_UP ) : ret = 1.2345 : scale = 4
//mc ( precision=4 roundingMode=HALF_UP ) : ret = 1.235 : scale = 3
//mc ( precision=3 roundingMode=HALF_UP ) : ret = 1.23 : scale = 2
//mc ( precision=2 roundingMode=HALF_UP ) : ret = 1.2 : scale = 1
byte byteValueExact ()
型の違い以外は、intValueExact() と同じになります。
そちらのAPI使用例をご参照ください。
int compareTo (BigDecimal val)
値のみで比較され、スケールの違いは考慮されません。
final var dec1 = new BigDecimal("111");
final var dec2 = new BigDecimal("222");
System.out.println(dec1.compareTo(dec2)); // -1
System.out.println(dec2.compareTo(dec1)); // 1
final var dec1 = new BigDecimal("1.23");
final var dec2 = new BigDecimal("-1.23");
System.out.println(dec1.compareTo(dec2)); // 1
System.out.println(dec2.compareTo(dec1)); // -1
final var dec1 = new BigDecimal("1234");
final var dec2 = new BigDecimal("1234");
System.out.println(dec1.compareTo(dec2)); // 0
System.out.println(dec2.compareTo(dec1)); // 0
final var dec1 = new BigDecimal("2.0");
System.out.println(dec1); // 2.0
System.out.println(dec1.scale()); // 1
final var dec2 = new BigDecimal("2.00");
System.out.println(dec2); // 2.00
System.out.println(dec2.scale()); // 2
System.out.println(dec1.compareTo(dec2)); // 0
System.out.println(dec2.compareTo(dec1)); // 0
BigDecimal divide (BigDecimal divisor)
final var dec = new BigDecimal("6");
final var divisor = new BigDecimal("2");
// 6 ÷ 2
final var ret = dec.divide(divisor);
System.out.println(ret); // 3
System.out.println(ret.scale()); // 0
final var dec = new BigDecimal("5");
final var divisor = new BigDecimal("4");
// 5 ÷ 4
final var ret = dec.divide(divisor);
System.out.println(ret); // 1.25
System.out.println(ret.scale()); // 2
final var dec = new BigDecimal("12");
final var divisor = new BigDecimal("-0.1");
// 12 ÷ -0.1
final var ret = dec.divide(divisor);
System.out.println(ret.toPlainString()); // -120
System.out.println(ret.scale()); // -1
// 10 ÷ 3
final var dec = BigDecimal.TEN;
final var divisor = new BigDecimal("3");
//ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
//dec.divide(divisor);
BigDecimal divide (BigDecimal divisor, int roundingMode)
非推奨です。
BigDecimal divide (BigDecimal divisor, int scale, int roundingMode)
非推奨です。
BigDecimal divide (BigDecimal divisor, int scale, RoundingMode roundingMode)
// 5 ÷ 4
final var dec = new BigDecimal("5");
final var divisor = new BigDecimal("4");
final var ret1 = dec.divide(divisor, 2, RoundingMode.UNNECESSARY);
System.out.println(ret1); // 1.25
System.out.println(ret1.scale()); // 2
final var ret2 = dec.divide(divisor, 1, RoundingMode.HALF_UP);
System.out.println(ret2); // 1.3
System.out.println(ret2.scale()); // 1
final var ret3 = dec.divide(divisor, 1, RoundingMode.FLOOR);
System.out.println(ret3); // 1.2
System.out.println(ret3.scale()); // 1
// 10 ÷ 3
final var dec = BigDecimal.TEN;
final var divisor = new BigDecimal("3");
final var ret = dec.divide(divisor, 5, RoundingMode.HALF_UP);
System.out.println(ret); // 3.33333
System.out.println(ret.scale()); // 5
BigDecimal divide (BigDecimal divisor, MathContext mc)
final var mcList = List.of(
new MathContext(3),
new MathContext(2),
new MathContext(1));
// 5 ÷ 4
final var dec = new BigDecimal("5");
final var divisor = new BigDecimal("4");
for (final var mc : mcList) {
final var ret = dec.divide(divisor, mc);
System.out.println("mc ( " + mc + " ) : ret = "
+ ret.toPlainString() + " : scale = " + ret.scale());
}
// 結果
// ↓
//mc ( precision=3 roundingMode=HALF_UP ) : ret = 1.25 : scale = 2
//mc ( precision=2 roundingMode=HALF_UP ) : ret = 1.3 : scale = 1
//mc ( precision=1 roundingMode=HALF_UP ) : ret = 1 : scale = 0
// 10 ÷ 3
final var dec = BigDecimal.TEN;
final var divisor = new BigDecimal("3");
final var ret = dec.divide(divisor, new MathContext(5, RoundingMode.CEILING));
System.out.println(ret); // 3.3334
System.out.println(ret.scale()); // 4
BigDecimal divide (BigDecimal divisor, RoundingMode roundingMode)
// 5 ÷ 4
final var dec = new BigDecimal("5.00");
final var divisor = new BigDecimal("4");
final var ret1 = dec.divide(divisor, RoundingMode.UNNECESSARY);
System.out.println(ret1); // 1.25
System.out.println(ret1.scale()); // 2
// 5 ÷ 4
final var dec = new BigDecimal("5.0");
final var divisor = new BigDecimal("4");
final var ret1 = dec.divide(divisor, RoundingMode.CEILING);
System.out.println(ret1); // 1.3
System.out.println(ret1.scale()); // 1
final var ret2 = dec.divide(divisor, RoundingMode.FLOOR);
System.out.println(ret2); // 1.2
System.out.println(ret2.scale()); // 1
// 5 ÷ 4
final var dec = new BigDecimal("5");
final var divisor = new BigDecimal("4.00");
final var ret1 = dec.divide(divisor, RoundingMode.HALF_UP);
System.out.println(ret1); // 1
System.out.println(ret1.scale()); // 0
// 10 ÷ 3
final var dec = new BigDecimal("10.000");
final var divisor = new BigDecimal("3");
final var ret = dec.divide(divisor, RoundingMode.FLOOR);
System.out.println(ret); // 3.333
System.out.println(ret.scale()); // 3
BigDecimal[] divideAndRemainder (BigDecimal divisor)
divideToIntegralValue(BigDecimal divisor) と remainder(BigDecimal divisor) の使用例もご参照ください。
// 11 ÷ 3
final var dec = new BigDecimal("11");
final var divisor = new BigDecimal("3");
final var ret = dec.divideAndRemainder(divisor);
System.out.println(ret.length); // 2
// 商
System.out.println(ret[0]); // 3
System.out.println(ret[0].scale()); // 0
// 余り
System.out.println(ret[1]); // 2
System.out.println(ret[1].scale()); // 0
BigDecimal[] divideAndRemainder (BigDecimal divisor, MathContext mc)
divideToIntegralValue(BigDecimal divisor, MathContext mc) と remainder(BigDecimal divisor, MathContext mc) の使用例もご参照ください。
final var mcList = List.of(
new MathContext(3),
new MathContext(2),
new MathContext(1));
// 5000 ÷ 400(商:12、余り:200)
final var dec = new BigDecimal("5000.0");
final var divisor = new BigDecimal("400");
for (final var mc : mcList) {
try {
System.out.println("mc ( " + mc + " )");
final var ret = dec.divideAndRemainder(divisor, mc);
System.out.println(" ret[0] = " + ret[0].toPlainString() + " : scale = " + ret[0].scale());
System.out.println(" ret[1] = " + ret[1].toPlainString() + " : scale = " + ret[1].scale());
} catch (ArithmeticException e) {
System.out.println(" ArithmeticException!");
}
}
// 結果
// ↓
//mc ( precision=3 roundingMode=HALF_UP )
// ret[0] = 12.0 : scale = 1
// ret[1] = 200.0 : scale = 1
//mc ( precision=2 roundingMode=HALF_UP )
// ret[0] = 12 : scale = 0
// ret[1] = 200.0 : scale = 1
//mc ( precision=1 roundingMode=HALF_UP )
// ArithmeticException!
BigDecimal divideToIntegralValue (BigDecimal divisor)
// 10 ÷ 2
final var dec = new BigDecimal("10");
final var divisor = new BigDecimal("2");
final var ret = dec.divideToIntegralValue(divisor);
System.out.println(ret); // 5
System.out.println(ret.scale()); // 0
// 50 ÷ 4
final var dec = new BigDecimal("50.0");
final var divisor = new BigDecimal("4");
final var ret = dec.divideToIntegralValue(divisor);
System.out.println(ret); // 12.0
System.out.println(ret.scale()); // 1
// 10 ÷ 3
final var dec = BigDecimal.TEN;
final var divisor = new BigDecimal("3");
final var ret = dec.divideToIntegralValue(divisor);
System.out.println(ret); // 3
System.out.println(ret.scale()); // 0
BigDecimal divideToIntegralValue (BigDecimal divisor, MathContext mc)
final var mcList = List.of(
new MathContext(3),
new MathContext(2),
new MathContext(1));
// 50 ÷ 4
final var dec = new BigDecimal("50.0");
final var divisor = new BigDecimal("4");
for (final var mc : mcList) {
try {
final var ret = dec.divideToIntegralValue(divisor, mc);
System.out.println("mc (" + mc + ") : ret = "
+ ret.toPlainString() + " : scale = " + ret.scale());
} catch (ArithmeticException e) {
System.out.println("mc (" + mc + ") : ArithmeticException!");
}
}
// 結果
// ↓
//mc (precision=3 roundingMode=HALF_UP) : ret = 12.0 : scale = 1
//mc (precision=2 roundingMode=HALF_UP) : ret = 12 : scale = 0
//mc (precision=1 roundingMode=HALF_UP) : ArithmeticException!
double doubleValue ()
プリミティブ型に変換した時点で、誤差が発生する可能性があるのでご注意ください。
final var dec = new BigDecimal("1.5");
System.out.println(dec); // 1.5
final double ret = dec.doubleValue();
System.out.println(ret); // 1.5
// BigDecimalで再変換して正確な値を確認。
System.out.println(new BigDecimal(ret)); // 1.5
final var dec = new BigDecimal("0.1");
System.out.println(dec); // 0.1
// 誤差が発生する例です。
final double ret = dec.doubleValue();
System.out.println(ret); // 0.1
// BigDecimalで再変換して正確な値を確認。
System.out.println(new BigDecimal(ret)); // 0.1000000000000000055511151231257827021181583404541015625
boolean equals (Object x)
equalsはスケールも含めて比較します。
スケールを含めず比較したい場合は、compareTo(BigDecimal val)をご使用ください。
final var dec1 = new BigDecimal("111");
final var dec2 = new BigDecimal("222");
System.out.println(dec1.equals(dec2)); // false
final var dec1 = new BigDecimal("1234");
final var dec2 = new BigDecimal("1234");
System.out.println(dec1.equals(dec2)); // true
final var dec1 = new BigDecimal("1.23");
final var dec2 = new BigDecimal("-1.23");
System.out.println(dec1.equals(dec2)); // false
final var dec1 = new BigDecimal("2.0");
final var dec2 = new BigDecimal("2.00");
System.out.println(dec1.equals(dec2)); // false
float floatValue ()
final var dec = new BigDecimal("1.5");
System.out.println(dec); // 1.5
final float ret = dec.floatValue();
System.out.println(ret); // 1.5
// BigDecimalで再変換して正確な値を確認。
System.out.println(new BigDecimal(ret)); // 1.5
// 誤差が発生する例です。
final var dec = new BigDecimal("0.1");
System.out.println(dec); // 0.1
final float ret = dec.floatValue();
System.out.println(ret); // 0.1
// BigDecimalで再変換して正確な値を確認。
System.out.println(new BigDecimal(ret)); // 0.100000001490116119384765625
// 無限大になる例です。
final var dec = new BigDecimal("1234567890123456789012345678901234567890");
System.out.println(dec); // 1.5
final float ret = dec.floatValue();
System.out.println(ret); // Infinity
int hashCode ()
final var dec1 = new BigDecimal("111");
final var dec2 = new BigDecimal("222");
System.out.println(dec1.hashCode()); // 3441
System.out.println(dec2.hashCode()); // 6882
final var dec1 = new BigDecimal("1234");
final var dec2 = new BigDecimal("1234");
System.out.println(dec1 != dec2); // true
System.out.println(dec1.hashCode()); // 38254
System.out.println(dec2.hashCode()); // 38254
final var dec1 = new BigDecimal("1.23");
final var dec2 = new BigDecimal("-1.23");
System.out.println(dec1.hashCode()); // 3815
System.out.println(dec2.hashCode()); // -3811
final var dec1 = new BigDecimal("2.0");
final var dec2 = new BigDecimal("2.00");
System.out.println(dec1.hashCode()); // 621
System.out.println(dec2.hashCode()); // 6202
int intValue ()
final var dec = new BigDecimal("12345");
System.out.println(dec); // 12345
final int ret = dec.intValue();
System.out.println(ret); // 12345
final var dec = new BigDecimal("12.345");
System.out.println(dec); // 12.345
final int ret = dec.intValue();
System.out.println(ret); // 12
System.out.println(Integer.MAX_VALUE); // 2147483647
final var dec1 = new BigDecimal("2147483647");
final var dec2 = new BigDecimal("2147483648");
System.out.println(dec1); // 2147483647
System.out.println(dec2); // 2147483648
System.out.println(dec1.intValue()); // 2147483647
System.out.println(dec2.intValue()); // -2147483648
int intValueExact ()
final var dec = new BigDecimal("12345");
System.out.println(dec); // 12345
final int ret = dec.intValueExact();
System.out.println(ret); // 12345
final var dec = new BigDecimal("12.345");
System.out.println(dec); // 12.345
//dec.intValueExact(); // ArithmeticException: Rounding necessary
System.out.println(Integer.MAX_VALUE); // 2147483647
final var dec1 = new BigDecimal("2147483647");
final var dec2 = new BigDecimal("2147483648");
System.out.println(dec1); // 2147483647
System.out.println(dec2); // 2147483648
System.out.println(dec1.intValue()); // 2147483647
//dec2.intValueExact(); // ArithmeticException: Overflow
long longValue ()
型の違い以外は、intValue() と同じになります。
そちらのAPI使用例をご参照ください。
long longValueExact ()
型の違い以外は、intValueExact() と同じになります。
そちらのAPI使用例をご参照ください。
BigDecimal max (BigDecimal val)
final var dec1 = new BigDecimal("123");
final var dec2 = new BigDecimal("456");
System.out.println(dec1.max(dec2)); // 456
System.out.println(dec2.max(dec1)); // 456
final var dec1 = new BigDecimal("1.234");
final var dec2 = new BigDecimal("-1.234");
System.out.println(dec1.max(dec2)); // 1.234
System.out.println(dec2.max(dec1)); // 1.234
final var dec1 = new BigDecimal("2.00");
final var dec2 = new BigDecimal("2.0");
System.out.println(dec1.max(dec2)); // 2.00
System.out.println(dec2.max(dec1)); // 2.0
BigDecimal min (BigDecimal val)
final var dec1 = new BigDecimal("123");
final var dec2 = new BigDecimal("456");
System.out.println(dec1.min(dec2)); // 123
System.out.println(dec2.min(dec1)); // 123
final var dec1 = new BigDecimal("1.234");
final var dec2 = new BigDecimal("-1.234");
System.out.println(dec1.min(dec2)); // -1.234
System.out.println(dec2.min(dec1)); // -1.234
final var dec1 = new BigDecimal("2.00");
final var dec2 = new BigDecimal("2.0");
System.out.println(dec1.min(dec2)); // 2.00
System.out.println(dec2.min(dec1)); // 2.0
BigDecimal movePointLeft (int n)
final var dec = new BigDecimal("1");
System.out.println(dec); // 1
System.out.println(dec.movePointLeft(0)); // 1
System.out.println(dec.movePointLeft(1)); // 0.1
System.out.println(dec.movePointLeft(2)); // 0.01
System.out.println(dec.movePointLeft(3)); // 0.001
System.out.println(dec.movePointLeft(4)); // 0.0001
final var dec = new BigDecimal("-1.234");
System.out.println(dec); // -1.234
System.out.println(dec.movePointLeft(0)); // -1.234
System.out.println(dec.movePointLeft(1)); // -0.1234
System.out.println(dec.movePointLeft(2)); // -0.01234
System.out.println(dec.movePointLeft(3)); // -0.001234
System.out.println(dec.movePointLeft(4)); // -0.0001234
BigDecimal movePointRight (int n)
final var dec = new BigDecimal("1");
System.out.println(dec); // 1
System.out.println(dec.movePointRight(0)); // 1
System.out.println(dec.movePointRight(1)); // 10
System.out.println(dec.movePointRight(2)); // 100
System.out.println(dec.movePointRight(3)); // 1000
System.out.println(dec.movePointRight(4)); // 10000
final var dec = new BigDecimal("-1.234");
System.out.println(dec); // -1.234
System.out.println(dec.movePointRight(0)); // -1.234
System.out.println(dec.movePointRight(1)); // -12.34
System.out.println(dec.movePointRight(2)); // -123.4
System.out.println(dec.movePointRight(3)); // -1234
System.out.println(dec.movePointRight(4)); // -12340
BigDecimal multiply (BigDecimal multiplicand)
// 100 × 23
final var dec = new BigDecimal("100");
final var multiplicand = new BigDecimal("23");
final var ret = dec.multiply(multiplicand);
System.out.println(ret); // 2300
// 100 × -23
final var dec = new BigDecimal("100");
final var multiplicand = new BigDecimal("-23");
final var ret = dec.multiply(multiplicand);
System.out.println(ret); // -2300
// 0.123 × 0.1
final var dec = new BigDecimal("0.123");
final var multiplicand = new BigDecimal("0.1");
final var ret = dec.multiply(multiplicand);
System.out.println(ret); // 0.0123
BigDecimal multiply (BigDecimal multiplicand, MathContext mc)
final var mcList = List.of(
new MathContext(4),
new MathContext(3),
new MathContext(2),
new MathContext(1));
// 1234 × 2
final var dec = new BigDecimal("1234");
final var multiplicand = new BigDecimal("2");
for (final var mc : mcList) {
final var ret = dec.multiply(multiplicand, mc);
System.out.println("mc ( " + mc + " ) : ret = "
+ ret.toPlainString() + " : scale = " + ret.scale());
}
// 結果
// ↓
//mc ( precision=4 roundingMode=HALF_UP ) : ret = 2468 : scale = 0
//mc ( precision=3 roundingMode=HALF_UP ) : ret = 2470 : scale = -1
//mc ( precision=2 roundingMode=HALF_UP ) : ret = 2500 : scale = -2
//mc ( precision=1 roundingMode=HALF_UP ) : ret = 2000 : scale = -3
BigDecimal negate ()
final var dec = new BigDecimal("1234");
System.out.println(dec.negate()); // -1234
final var dec = new BigDecimal("-1.234");
System.out.println(dec.negate()); // 1.234
final var dec = new BigDecimal("2.00");
System.out.println(dec.negate()); // -2.00
BigDecimal negate (MathContext mc)
final var mcList = List.of(
new MathContext(5),
new MathContext(4),
new MathContext(3),
new MathContext(2));
final var dec = new BigDecimal("12345");
for (final var mc : mcList) {
final var ret = dec.negate(mc);
System.out.println("mc ( " + mc + " ) : ret = "
+ ret.toPlainString() + " : scale = " + ret.scale());
}
// 結果
// ↓
//mc ( precision=5 roundingMode=HALF_UP ) : ret = -12345 : scale = 0
//mc ( precision=4 roundingMode=HALF_UP ) : ret = -12350 : scale = -1
//mc ( precision=3 roundingMode=HALF_UP ) : ret = -12300 : scale = -2
//mc ( precision=2 roundingMode=HALF_UP ) : ret = -12000 : scale = -3
BigDecimal plus ()
final var dec = new BigDecimal("1234");
System.out.println(dec.plus()); // 1234
final var dec = new BigDecimal("-1.234");
System.out.println(dec.plus()); // -1.234
final var dec = new BigDecimal("2.00");
System.out.println(dec.plus()); // 2.00
BigDecimal plus (MathContext mc)
final var mcList = List.of(
new MathContext(5),
new MathContext(4),
new MathContext(3),
new MathContext(2));
final var dec = new BigDecimal("12345");
for (final var mc : mcList) {
final var ret = dec.plus(mc);
System.out.println("mc ( " + mc + " ) : ret = "
+ ret.toPlainString() + " : scale = " + ret.scale());
}
// 結果
// ↓
//mc ( precision=5 roundingMode=HALF_UP ) : ret = 12345 : scale = 0
//mc ( precision=4 roundingMode=HALF_UP ) : ret = 12350 : scale = -1
//mc ( precision=3 roundingMode=HALF_UP ) : ret = 12300 : scale = -2
//mc ( precision=2 roundingMode=HALF_UP ) : ret = 12000 : scale = -3
BigDecimal pow (int n)
final var dec = new BigDecimal("2");
System.out.println(dec.pow(0)); // 1
System.out.println(dec.pow(1)); // 2
System.out.println(dec.pow(2)); // 4
System.out.println(dec.pow(3)); // 8
System.out.println(dec.pow(4)); // 16
System.out.println(dec.pow(5)); // 32
System.out.println(dec.pow(6)); // 64
System.out.println(BigDecimal.ZERO.pow(0)); // 1
BigDecimal pow (int n, MathContext mc)
final var mcList = List.of(
new MathContext(5),
new MathContext(4),
new MathContext(3),
new MathContext(2));
final var dec = new BigDecimal("2");
for (final var mc : mcList) {
// 2 の 16乗
final var ret = dec.pow(16, mc);
System.out.println("mc ( " + mc + " ) : ret = "
+ ret.toPlainString() + " : scale = " + ret.scale());
}
// 結果
// ↓
//mc ( precision=5 roundingMode=HALF_UP ) : ret = 65536 : scale = 0
//mc ( precision=4 roundingMode=HALF_UP ) : ret = 65540 : scale = -1
//mc ( precision=3 roundingMode=HALF_UP ) : ret = 65500 : scale = -2
//mc ( precision=2 roundingMode=HALF_UP ) : ret = 66000 : scale = -3
int precision ()
System.out.println(new BigDecimal("0").precision()); // 1
System.out.println(new BigDecimal("1").precision()); // 1
System.out.println(new BigDecimal("12345").precision()); // 5
System.out.println(new BigDecimal("-67890").precision()); // 5
System.out.println(new BigDecimal("0.123").precision()); // 3
System.out.println(new BigDecimal("1.234").precision()); // 4
System.out.println(new BigDecimal("12.3456").precision()); // 6
final var dec1 = new BigDecimal("12345");
System.out.println(dec1.toPlainString()); // 12345
System.out.println(dec1.scale()); // 0
System.out.println(dec1.precision()); // 5
final var dec2 = dec1.setScale(-3, RoundingMode.FLOOR);
System.out.println(dec2.toPlainString()); // 12000
System.out.println(dec2.scale()); // -3
System.out.println(dec2.precision()); // 2
BigDecimal remainder (BigDecimal divisor)
// 5 % 3
final var dec = new BigDecimal("5.0");
final var divisor = new BigDecimal("3");
final var ret = dec.remainder(divisor);
System.out.println(ret); // 2.0
System.out.println(ret.scale()); // 1
// 15 % -6
final var dec = new BigDecimal("15");
final var divisor = new BigDecimal("-6");
final var ret = dec.remainder(divisor);
System.out.println(ret); // 3
System.out.println(ret.scale()); // 0
// 10 % 0.03
final var dec = new BigDecimal("10");
final var divisor = new BigDecimal("0.03");
final var ret = dec.remainder(divisor);
System.out.println(ret); // 0.01
System.out.println(ret.scale()); // 2
BigDecimal remainder (BigDecimal divisor, MathContext mc)
補足
- MathContextの精度(precision)は、結果となる剰余そのもには影響しません。
剰余を算出する過程(divideToIntegralValueの呼び出し)で使われます。
詳細はAPI仕様をご確認ください。
結果となる剰余そのものにMathContextが影響しない例です。
final var mcList = List.of(
new MathContext(3),
new MathContext(2),
new MathContext(1));
// 1000 ÷ 877 (商:1、余り:123)
final var dec = new BigDecimal("1000");
final var divisor = new BigDecimal("877");
for (final var mc : mcList) {
final var ret = dec.remainder(divisor, mc);
System.out.println("mc ( " + mc + " ) : ret = "
+ ret.toPlainString() + " : scale = " + ret.scale());
}
// 結果
// ↓
//mc ( precision=3 roundingMode=HALF_UP ) : ret = 123 : scale = 0
//mc ( precision=2 roundingMode=HALF_UP ) : ret = 123 : scale = 0
//mc ( precision=1 roundingMode=HALF_UP ) : ret = 123 : scale = 0
剰余を算出するための divideToIntegralValue で ArithmeticExceptionが発生する例です。
final var mcList = List.of(
new MathContext(3),
new MathContext(2),
new MathContext(1));
// 100 ÷ 3 (商:33、余り:1)
final var dec = new BigDecimal("100");
final var divisor = new BigDecimal("3");
for (final var mc : mcList) {
try {
final var ret = dec.remainder(divisor, mc);
System.out.println("mc ( " + mc + " ) : ret = "
+ ret.toPlainString() + " : scale = " + ret.scale());
} catch (ArithmeticException e) {
System.out.println("mc ( " + mc + " ) : ArithmeticException!");
}
}
// 結果
// ↓
//mc ( precision=3 roundingMode=HALF_UP ) : ret = 1 : scale = 0
//mc ( precision=2 roundingMode=HALF_UP ) : ret = 1 : scale = 0
//mc ( precision=1 roundingMode=HALF_UP ) : ArithmeticException!
BigDecimal round (MathContext mc)
final var dec = new BigDecimal("1.2345");
// 切り上げ(正の無限大に近づくように丸める)
final var ret1 = dec.round(new MathContext(4, RoundingMode.CEILING));
System.out.println(ret1); // 1.235
System.out.println(ret1.scale()); // 3
// 切り捨て(負の無限大に近づくように丸める)
final var ret2 = dec.round(new MathContext(4, RoundingMode.FLOOR));
System.out.println(ret2); // 1.234
System.out.println(ret2.scale()); // 3
final var ret3 = dec.round(new MathContext(3, RoundingMode.FLOOR));
System.out.println(ret3); // 1.23
System.out.println(ret3.scale()); // 2
final var dec = new BigDecimal("12345");
final var ret1 = dec.round(new MathContext(4, RoundingMode.CEILING));
System.out.println(ret1.toPlainString()); // 12350
System.out.println(ret1.scale()); // -1
final var ret2 = dec.round(new MathContext(3, RoundingMode.FLOOR));
System.out.println(ret2.toPlainString()); // 12300
System.out.println(ret2.scale()); // -2
int scale ()
final var dec = new BigDecimal("1234");
System.out.println(dec); // 1234
System.out.println(dec.scale()); // 0
final var dec = new BigDecimal("1.234");
System.out.println(dec); // 1.234
System.out.println(dec.scale()); // 3
final var dec = new BigDecimal("12.3E+7");
System.out.println(dec.toPlainString()); // 123000000
System.out.println(dec.scale()); // -6
final var dec = new BigDecimal("12345", new MathContext(3));
System.out.println(dec.toPlainString()); // 12300
System.out.println(dec.scale()); // -2
BigDecimal scaleByPowerOfTen (int n)
final var dec = new BigDecimal("1234");
System.out.println(dec); // 1234
System.out.println(dec.scale()); // 0
final var ret1 = dec.scaleByPowerOfTen(1);
System.out.println(ret1.toPlainString()); // 12340
System.out.println(ret1.scale()); // -1
final var ret2 = dec.scaleByPowerOfTen(2);
System.out.println(ret2.toPlainString()); // 123400
System.out.println(ret2.scale()); // -2
final var dec = new BigDecimal("1.234");
System.out.println(dec); // 1.234
System.out.println(dec.scale()); // 3
final var ret1 = dec.scaleByPowerOfTen(-1);
System.out.println(ret1.toPlainString()); // 0.1234
System.out.println(ret1.scale()); // 4
final var ret2 = dec.scaleByPowerOfTen(-2);
System.out.println(ret2.toPlainString()); // 0.01234
System.out.println(ret2.scale()); // 5
BigDecimal setScale (int newScale)
final var dec = new BigDecimal("1234");
System.out.println(dec); // 1234
System.out.println(dec.scale()); // 0
final var ret1 = dec.setScale(1);
System.out.println(ret1.toPlainString()); // 1234.0
System.out.println(ret1.scale()); // 1
final var ret2 = dec.setScale(2);
System.out.println(ret2.toPlainString()); // 1234.00
System.out.println(ret2.scale()); // 2
//dec.setScale(-1); // ArithmeticException: Rounding necessary
BigDecimal setScale (int newScale, int roundingMode)
非推奨です。
BigDecimal setScale (int newScale, RoundingMode roundingMode)
final var dec = new BigDecimal("1.2345");
System.out.println(dec); // 1.2345
System.out.println(dec.scale()); // 4
final var ret1 = dec.setScale(3, RoundingMode.CEILING);
System.out.println(ret1.toPlainString()); // 1.235
System.out.println(ret1.scale()); // 3
final var ret2 = dec.setScale(3, RoundingMode.FLOOR);
System.out.println(ret2.toPlainString()); // 1.234
System.out.println(ret2.scale()); // 3
final var dec = new BigDecimal("12345");
System.out.println(dec); // 12345
System.out.println(dec.scale()); // 0
final var ret1 = dec.setScale(-1, RoundingMode.HALF_UP);
System.out.println(ret1.toPlainString()); // 12350
System.out.println(ret1.scale()); // -1
final var ret2 = dec.setScale(-2, RoundingMode.HALF_UP);
System.out.println(ret2.toPlainString()); // 12300
System.out.println(ret2.scale()); // -2
short shortValueExact ()
型の違い以外は、intValueExact() と同じになります。
そちらのAPI使用例をご参照ください。
int signum ()
System.out.println(new BigDecimal("123").signum()); // 1
System.out.println(new BigDecimal("-456").signum()); // -1
System.out.println(new BigDecimal("1.23").signum()); // 1
System.out.println(new BigDecimal("-9.99").signum()); // -1
System.out.println(BigDecimal.ZERO.signum()); // 0
BigDecimal sqrt (MathContext mc)
final var dec = new BigDecimal("2");
// √2
final var ret1 = dec.sqrt(new MathContext(10));
System.out.println(ret1.toPlainString()); // 1.414213562
System.out.println(ret1.scale()); // 9
final var ret2 = dec.sqrt(new MathContext(5));
System.out.println(ret2.toPlainString()); // 1.4142
System.out.println(ret2.scale()); // 4
final var ret = BigDecimal.ZERO.sqrt(new MathContext(10));
System.out.println(ret.toPlainString()); // 0
System.out.println(BigDecimal.ZERO.equals(ret)); // true
BigDecimal stripTrailingZeros ()
final var dec = new BigDecimal("3.00");
System.out.println(dec.toPlainString()); // 3.00
System.out.println(dec.scale()); // 2
final var ret = dec.stripTrailingZeros();
System.out.println(ret.toPlainString()); // 3
System.out.println(ret.scale()); // 0
final var dec = new BigDecimal("123400");
System.out.println(dec.toPlainString()); // 123400
System.out.println(dec.scale()); // 0
final var ret = dec.stripTrailingZeros();
System.out.println(ret.toPlainString()); // 123400
System.out.println(ret.scale()); // -2
final var dec = new BigDecimal("1.2340");
System.out.println(dec.toPlainString()); // 1.2340
System.out.println(dec.scale()); // 4
final var ret = dec.stripTrailingZeros();
System.out.println(ret.toPlainString()); // 1.234
System.out.println(ret.scale()); // 3
BigDecimal subtract (BigDecimal subtrahend)
final var dec = new BigDecimal("100");
final var subtrahend = new BigDecimal("23");
final var ret = dec.subtract(subtrahend);
System.out.println(ret); // 77
final var dec = new BigDecimal("100");
final var subtrahend = new BigDecimal("-23");
final var ret = dec.subtract(subtrahend);
System.out.println(ret); // 123
final var dec = new BigDecimal("0.1");
final var subtrahend = new BigDecimal("0.023");
final var ret = dec.subtract(subtrahend);
System.out.println(ret); // 0.077
BigDecimal subtract (BigDecimal subtrahend, MathContext mc)
final var mcList = List.of(
new MathContext(5),
new MathContext(4),
new MathContext(3),
new MathContext(2));
// 1.12345 - 1
final var dec = new BigDecimal("1.12345");
final var subtrahend = new BigDecimal("1");
for (final var mc : mcList) {
final var ret = dec.subtract(subtrahend, mc);
System.out.println("mc ( " + mc + " ) : ret = "
+ ret.toPlainString() + " : scale = " + ret.scale());
}
// 結果
// ↓
//mc ( precision=5 roundingMode=HALF_UP ) : ret = 0.12345 : scale = 5
//mc ( precision=4 roundingMode=HALF_UP ) : ret = 0.1235 : scale = 4
//mc ( precision=3 roundingMode=HALF_UP ) : ret = 0.123 : scale = 3
//mc ( precision=2 roundingMode=HALF_UP ) : ret = 0.12 : scale = 2
BigInteger toBigInteger ()
final var dec = new BigDecimal("12345");
System.out.println(dec); // 12345
final var ret = dec.toBigInteger();
System.out.println(ret); // 12345
final var dec = new BigDecimal("123.4567");
System.out.println(dec); // 123.4567
final var ret = dec.toBigInteger();
System.out.println(ret); // 123
final var dec = new BigDecimal("1234567890123456789012345678901234567890");
System.out.println(dec); // 1234567890123456789012345678901234567890
final var ret = dec.toBigInteger();
System.out.println(ret); // 1234567890123456789012345678901234567890
BigInteger toBigIntegerExact ()
final var dec = new BigDecimal("12345");
System.out.println(dec); // 12345
final var ret = dec.toBigIntegerExact();
System.out.println(ret); // 12345
final var dec = new BigDecimal("123.4567");
System.out.println(dec); // 123.4567
//dec.toBigIntegerExact(); // ArithmeticException: Rounding necessary
String toEngineeringString ()
toEngineeringString の使用例は、toString() にまとめて記載しました。
そちらのAPI使用例をご参照ください。
String toPlainString ()
toPlainString の使用例は、toString() にまとめて記載しました。
そちらのAPI使用例をご参照ください。
String toString ()
final var dec = new BigDecimal("123456789");
System.out.println(dec.toPlainString()); // 123456789
System.out.println(dec.toString()); // 123456789
System.out.println(dec.toEngineeringString()); // 123456789
final var dec2 = dec.setScale(-2, RoundingMode.FLOOR);
System.out.println(dec2.toPlainString()); // 123456700
System.out.println(dec2.toString()); // 1.234567E+8
System.out.println(dec2.toEngineeringString()); // 123.4567E+6
BigDecimal ulp ()
System.out.println(new BigDecimal("3").ulp()); // 1
System.out.println(new BigDecimal("3.0").ulp()); // 0.1
System.out.println(new BigDecimal("3.00").ulp()); // 0.01
System.out.println(new BigDecimal("30").ulp()); // 1
System.out.println(new BigDecimal("300").ulp()); // 1
final var dec1 = BigDecimal.valueOf(3, -1);
System.out.println(dec1.toPlainString()); // 30
System.out.println(dec1.scale()); // -1
System.out.println(dec1.ulp().toPlainString()); // 10
final var dec2 = BigDecimal.valueOf(3, -2);
System.out.println(dec2.toPlainString()); // 300
System.out.println(dec2.scale()); // -2
System.out.println(dec2.ulp().toPlainString()); // 100
BigInteger unscaledValue ()
final var dec = new BigDecimal("1.234");
System.out.println(dec); // 1.234
System.out.println(dec.scale()); // 3
final var ret = dec.unscaledValue();
System.out.println(ret); // 1234
final var dec = BigDecimal.valueOf(1234, -3);
System.out.println(dec.toPlainString()); // 1234000
System.out.println(dec.scale()); // -3
final var ret = dec.unscaledValue();
System.out.println(ret); // 1234
static BigDecimal valueOf (double val)
doubleの丸め誤差が完全になくなるわけではないのでご注意ください。
final double val = 0.1;
final var dec1 = BigDecimal.valueOf(val);
System.out.println(dec1); // 0.1
final var dec2 = new BigDecimal(val);
System.out.println(dec2); // 0.1000000000000000055511151231257827021181583404541015625
final double val = 0.1 + 0.2;
final var dec1 = BigDecimal.valueOf(val);
System.out.println(dec1); // 0.30000000000000004
final var dec2 = new BigDecimal(val);
System.out.println(dec2); // 0.3000000000000000444089209850062616169452667236328125
static BigDecimal valueOf (long val)
final var dec1 = BigDecimal.valueOf(1234L);
System.out.println(dec1); // 1234
final var dec2 = BigDecimal.valueOf(-56789L);
System.out.println(dec2); // -56789
static BigDecimal valueOf (long unscaledVal, int scale)
final var dec1 = BigDecimal.valueOf(1234L, 1);
System.out.println(dec1.toPlainString()); // 123.4
System.out.println(dec1.scale()); // 1
final var dec2 = BigDecimal.valueOf(1234L, 2);
System.out.println(dec2.toPlainString()); // 12.34
System.out.println(dec2.scale()); // 2
final var dec3 = BigDecimal.valueOf(1234L, 3);
System.out.println(dec3.toPlainString()); // 1.234
System.out.println(dec3.scale()); // 3
final var dec1 = BigDecimal.valueOf(1234L, -1);
System.out.println(dec1.toPlainString()); // 12340
System.out.println(dec1.scale()); // -1
final var dec2 = BigDecimal.valueOf(1234L, -2);
System.out.println(dec2.toPlainString()); // 123400
System.out.println(dec2.scale()); // -2
Numberで宣言されたメソッド
byteValue, shortValue
「Java API 使用例 : Number」をご参照ください。