Formatting Numbers

double value = 1623542.765;
											NumberFormat numberFormatter;
											String formattedValue;
											numberFormatter = NumberFormat.getNumberInstance();
											formattedValue = numberFormatter.format(value);
											System.out.format("%s%n", formattedValue);

In most applications, there is a need to display numbers. Fortunately, Java has built-in support for formatting numbers so that they will look as you want them to when you display them in your application.

This phrase will generate the following formatted number as output:

1,623,542.765

In this phrase, we use the NumberFormat class to format a double value into a comma separated string representation of its value. The NumberFormat class is found in the java.text package and is also very useful for code that ...

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.