Got a Double object that you need to format for a UI? No problem, I'll show you how to do it here with String.format().
In fact, I'll show you several ways to format that number. Then you can pick the option that best suits your business requirements.
Because I'm cool like that.
Rounding & Slicing
Sometimes, especially in complex math operations, you end up with a Double that has enough numbers after the decimal point to fill the Grand Canyon.
But you don't need to show the user all those numbers. You just need to display the Double with two or three decimal places.
That's easy to do.
Let's say you've got the following number:
And you need to trim that bad boy down to two decimal spots. Here's some code for you:
Double num = 45387.09809321845d; String formatted = String.format("%.2f", num); System.err.println(formatted);
Yeah, I'm using System.err instead of System.out because I always liked those red characters.
Anyhoo, the way to format the Double is to convert it to a formatted String. Then you can display it as you see fit on the UI.
That's accomplished with the format() static method that's part of the String class.
The format() method accepts two parameters. The first one is a String that describes how you want to format the number. The second parameter is the number itself.
That first parameter, or the format specifier, always begins with a percent sign. You see that in the code above.
The text that follows the percent sign specifies the nature of the formatting.
In the example above, you see the .2 that follows the percent sign. That means exactly what you think it means: format the number to two decimal places.
The f that follows the .2 means "display the number in floating point format."
So if you run the code above, the output will look like this:
Note that the code not only trimmed the number down to two decimal places, but it also rounded the number. So format() is a farily useful animal.
If you wanted to go out to four decimal places, you'd just rewrite that second line as follows:
String formatted = String.format("%.4f", num);
And the output would look like this:
Where Dem Commas?
It's often the case that you need user-friendly numeric output in your UI. The formatting above doesn't accomplish that.
Why? Because there are no commas in the large number preceding the decimal point.
You can fix that with this code:
Double num = 45387.09809321845d; String formatted = String.format("%,.4f", num); System.err.println(formatted);
That's going to give you this output:
Much better. That's how large numbers should look. Especially if you're developing an accounting application.
So what changed? Take a look at the first parameter in the format() method above. It's got a comma right after the percent sign.
That's what formats the number with commas every three decimal places.
Alas, not all regions use commas and periods the same way when displaying numbers. You might need to format your Double according to the rules of a specific country.
Here's how to format the output for Germany:
Double num = 45387.09809321845d; String formatted = String.format(Locale.GERMAN, "%,.4f", num); System.err.println(formatted);
Note that the code above uses an overloaded method of format() that takes three (3) parameters.
The first parameter is a Locale object. That specifies which region's rules the JVM should use when producing the output.
The second and third parameters are the format specifier and number, respectively.
Now if you run that code above, you'll get this output:
Cuz that's how they roll in Germany.
Perhaps you're developing an application for a world-class physicist and the target user group includes other physicists. In that case, you might opt for scientific notation in your output.
In that case, try something like this:
Double num = 45387.09809321845d; String formatted = String.format("%e", num); System.err.println(formatted);
That will give you this output:
Which is exactly what you'd expect, because that's what scientific notation looks like.
Scientific notation, by the way, is handled with use of e following the percent sign in the format specifier.
Wrapping It Up
There you go. Now you know how to use String.format() to format a Double.
And you've got mulitple options with different format specifiers as well.
Time to take what you've learned here and use it in your own application.
Photo by Magda Ehlers from Pexels