Got a Double object that you'd like to trim down to size or format for a UI? If so, then your favorite search engine just brought you to the right place.
I'll show you how to do that here with a class called DecimalFormat.
Let's get started.
When the Rounding Is Too Good
Let's say you've got a number that has a gajillion numbers after the decimal point. And you don't want to display all those numbers on the UI.
So your task is to trim that number down to just two or three decimal places. That's easy to do with DecimalFormat.
Let's look at an example. Consider this number:
That's too big. All you need to do is show just a couple of numbers after the decimal point.
Here's some code that will take care of that for you:
DecimalFormat df = new DecimalFormat("##,###.##"); Double num = 45387.09809321845d; System.err.println(df.format(num));
Take a look at the first line. It's pretty intuitive, isn't it?
It uses number signs/hashtags to describe how to format the number.
Now if you run that code above, you'll get this output:
Ah, so it formatted the number just like-
Wait. Why is there only one digit after the decimal point?
The pretty intuitive formatting I showed you above clearly has two number signs/hashtags following the decimal point. So what gives?
Here's what gives: the DecimalFormat object doesn't just truncate the numbers after the decimal point. It also performs basic rounding.
And that number rounds up to .10. But since 0 is (by default) unnecessary at the end of decimal numbers, it leaves the 0 off. The output becomes .1.
That's when the rounding is too good.
Rounding That Isn't Too Good
If you're writing an accounting application, you almost certainly don't want to display numbers like you see above. In that case, you'll need to do a different kind of formatting.
But you can still use DecimalFormat.
Instead of putting those number signs/hashtags at the end of the formatting string, just use zeroes instead. Like this:
DecimalFormat df = new DecimalFormat("##,###.00"); Double num = 45387.09809321845d; System.err.println(df.format(num));
Run that code and you'll get this result:
That's what your accountant users are looking for.
Looking for Locale
It's a global economy. As a result, you might need to write an application that formats numbers based on the user's location.
Fortunately, you've got an option there with DecimalFormat as well. It involves the use of DecimalFormatSymbols.
Let's say you got to do the same thing you did above but for a German audience. Try this on for size:
DecimalFormat df = new DecimalFormat("##,###.00", new DecimalFormatSymbols(Locale.GERMAN)); Double num = 45387.09809321845d; System.err.println(df.format(num));
The example above uses a different DecimalFormat constructor than what you saw in the last two code blocks.
The second parameter is a DecimalFormatSymbols object that gets instantiated with a Locale object. Specifically, it's using the Locale object returned by Locale.GERMAN because the formatting here is for a German audience.
Now run that code block and you'll get this output:
Chancellor Merkel would be proud!
Wrapping It Up
Now you know how to format a Double using DecimalFormat. You even know how to format the number for different regions.
Feel free to take what you've used here and incorporate it in your own Java development efforts.
Photo by from