1. Introduction
In this tutorial, we’ll take a look at the different ways to convert a double value to a String, removing its decimal places.
We’ll look at how to do it when we want to just truncate the decimal part and when we want to round it.
2. Truncation Using Casting
If our double value is within the int range, we can cast it to an int. The cast truncates the decimal part, meaning that it cuts it off without doing any rounding.
This approach is about 10 times as fast as the other approaches we’ll look at.
Once it’s an int, then we can then pass it to the valueOf method on the String class:
String truncated = String.valueOf((int) doubleValue);
We can confidently use this approach when we’re guaranteed that the double value is within the range of an int. But if our value exceeds that, casting won’t work like we’d want.
3. Rounding Using String.format()
Now, the remaining approaches aren’t as limited as casting, but they have their own nuances.
For example, another approach is to use the format method of the String class. The first parameter of the method specifies that we are formatting a floating point value with zero digits after the decimal point:
String rounded = String.format("%.0f", doubleValue);
The format method uses HALF_UP rounding which will round up if the value after the fractional part is .5 or above. Otherwise, it returns the number before the decimal point.
And while simple, String.format is the slowest way to do this.
4. Using NumberFormat.format()
The NumberFormat class also provides a format method similar to the String class, but NumberFormat is faster and with it, we can specify the rounding mode to achieve either truncation or rounding.
The setMaximumFractionDigits() method tells the formatter how many fractional digits after the decimal point to include in the output:
NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(0); String rounded = nf.format(doubleValue);
Curiously, NumberFormat doesn’t use HALF_UP by default. Instead, it uses HALF_EVEN rounding by default, meaning it will round like normal except at .5, in which case it will pick the nearest even number.
While HALF_EVEN is helpful with statistical analysis, let’s use HALF_UP to be consistent:
nf.setRoundingMode(RoundingMode.HALF_UP); String rounded = nf.format(doubleValue);
And, 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)
And now, it will truncate instead of round.
5. Using DecimalFormat.format()
Similar to NumberFormat, the DecimalFormat class can be used to format double values. However, instead of setting the output format with method calls, 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);
The “#,###” pattern signifies that we want the formatter to only return the integer part of the input. It also signals that we want the digits grouped in threes separated by a comma.
The same rounding defaults apply here so if we want to output a truncated value we can set the rounding mode to FLOOR:
df.setRoundingMode(RoundingMode.FLOOR); String truncated = df.format(doubleValue)
6. Using BigDecimal.toString()
The last approach we’ll look at is BigDecimal, which we’ll include because it out-performs NumberFormat and DecimalFormat for larger doubles.
We can use BigDecimal‘s setScale method to tell whether we want to round or truncate:
double largeDouble = 345_345_345_345.56; BigDecimal big = new BigDecimal(largeDouble); big = big.setScale(0, RoundingMode.HALF_UP);
Remember that BigDecimals are immutable so, like Strings, we need to reset the value.
And, then, we just call BigDecimal‘s toString:
String rounded = big.toString();
7. Conclusion
In this tutorial, we looked at the different ways in which we can convert a double to a String while removing decimal places. We provided approaches that would output either rounded or truncated values.
As usual, the samples and benchmarks are available over on GitHub.