doubleを文字列に変換し、小数点以下の桁数を削除する

DoubleからStringへの変換、小数点以下の桁の削除

1. 前書き

このチュートリアルでは、different ways to convert a double value to a String, removing its decimal places.を見ていきます。

小数部分を切り捨てる場合と丸める場合の方法を見ていきます。

2. 鋳造を使用した切り捨て

doubleの値がintの範囲内にある場合、cast it to an intキャストは小数部分を切り捨てることができます。つまり、丸めを行わずに小数部を切り捨てます。

このアプローチは、これから説明する他のアプローチの約10倍の速度です。

int,then we can then pass it to the valueOf methodon the String classになったら:

String truncated = String.valueOf((int) doubleValue);

the double value is within the range of an intが保証されている場合は、このアプローチを自信を持って使用できます。 しかし、if our value exceeds that, casting won’t work like we’d want

3. String.format()を使用した丸め

現在、残りのアプローチはキャスティングほど制限されていませんが、独自のニュアンスがあります。

たとえば、別のアプローチは、Stringクラスのformatメソッドを使用することです。 メソッドの最初のパラメーターは、小数点の後にゼロ桁の浮動小数点値をフォーマットすることを指定します。

String rounded = String.format("%.0f", doubleValue);

The format method uses HALF_UP roundingは、小数部の後の値が.5以上の場合に切り上げられます。 それ以外の場合は、小数点の前の数値を返します。

そして、単純ですが、String.formatis the slowest way to do thisです。

4. NumberFormat.format()の使用

NumberFormatクラスも、Stringクラスと同様のformatメソッドを提供しますが、NumberFormat is faster and with it, we can specify the rounding mode to achieve either truncation or rounding.

setMaximumFractionDigits()メソッドは、出力に含める小数点以下の小数桁数をフォーマッターに指示します。

NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(0);
String rounded = nf.format(doubleValue);

Curiously, NumberFormat doesn’t use HALF_UP by default.代わりに、デフォルトでHALF_EVENの丸めが使用されます。つまり、.5、in which case it will pick the nearest even numberを除いて、通常のように丸められます。

While HALF_EVEN is helpful with statistical analysis,は、一貫性を保つためにHALF_UPを使用しましょう。

nf.setRoundingMode(RoundingMode.HALF_UP);
String rounded = nf.format(doubleValue);

そして、we can change this and achieve truncation by setting the formatter to use the FLOOR rounding mode instead:

nf.setRoundingMode(RoundingMode.FLOOR);
String truncated = nf.format(doubleValue)

そして今、ラウンドではなくトランケートされます。

5. DecimalFormat.format()の使用

NumberFormatと同様に、DecimalFormatクラスを使用してdouble値をフォーマットできます。 ただし、メソッド呼び出しで出力形式を設定する代わりに、we can tell the formatter what output we want by providing the constructor with a specific pattern:

DecimalFormat df = new DecimalFormat("#,###");
df.setRoundingMode(RoundingMode.HALF_UP);
String rounded = df.format(doubleValue);

, ##」パターンは、フォーマッタが入力の整数部分のみを返すようにすることを意味します。 また、コンマで区切られた3桁にグループ化された数字が必要であることを示します。

ここでも同じ丸めのデフォルトが適用されるため、切り捨てられた値を出力する場合は、丸めモードをFLOORに設定できます。

df.setRoundingMode(RoundingMode.FLOOR);
String truncated = df.format(doubleValue)

6. BigDecimal.toString()の使用

最後に検討するアプローチはBigDecimalです。これは、it out-performs NumberFormat and DecimalFormat for larger doublesのために含めます。

BigDecimalsetScale メソッドを使用して、丸めるか切り捨てるかを指定できます。

double largeDouble = 345_345_345_345.56;
BigDecimal big = new BigDecimal(largeDouble);
big = big.setScale(0, RoundingMode.HALF_UP);

BigDecimalsは不変であるため、文字列と同様に、値をリセットする必要があることに注意してください。

そして、BigDecimaltoStringと呼ぶだけです。

String rounded = big.toString();

7. 結論

このチュートリアルでは、we looked at the different ways in which we can convert a double to a String while removing decimal places.丸められた値または切り捨てられた値のいずれかを出力するアプローチを提供しました。

いつものように、サンプルとベンチマークはover on GitHubで利用できます。