Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java format double 2 decimal places – currency formatting

farenda 2016-11-10 0

It’s very common task in Java to format double to 2 decimal places – usually to format currency. It’s not easy to do correctly, but in this post we’ll show how.

Round double using String.format

The simplest way to print double rounded to two decimal places is to use String.format as here:

double number = 12.5678;
String s = String.format("%.2f", number);
System.out.println(s);

It prints the rounded value:

12,57

Note the comma in the formatted string. It is because String.format is using the current Locale and in my case it is pl_PL. In English locale it would print dot instead of comma.

String.format prints leading zero

This way of formatting double preserves leading zero, unlike some other number formatting:

double number = 0.5678;
String s = String.format("%.2f", number);
System.out.println(s);

The above code produces the following output:

0,57

Round double using DecimalFormat

As String.format() above java.text.DecimalFormat is using default Locale to format numbers, so it will print formatted number with dot or comma as separator between integer and decimal parts:

DecimalFormat df = new DecimalFormat("#.##");
String s = df.format(1234.5678);
System.out.println(s);

## means that there should be at most 2 decimal numbers:

1234,57

DecimalFormat ## remove trailing zeros

Note that # in the format means that the digit at that position is optional and won’t be printed if it not needed. This happens with trailing zero here:

DecimalFormat df = new DecimalFormat("#.##");
String s = df.format(123.0);
System.out.println(s);

It prints:

123

DecimalFormat “00” to preserve trailing zeros

To force DecimalFormat to preserve specified number of trailing zeros use “00“:

DecimalFormat df = new DecimalFormat("#.00");
String s = df.format(123.0);
System.out.println(s);

Now double is formatted with trailing zeros:

123,00

Round double using NumberFormat – custom Locale

java.text.NumberFormat is the most flexible class, but also the most cumbersome to use. It allows to set Locale and specify number formatting parameters:

Locale locale = Locale.ENGLISH;

NumberFormat nf = NumberFormat.getNumberInstance(locale);
// for trailing zeros:
nf.setMinimumFractionDigits(2);
// round to 2 digits:
nf.setMaximumFractionDigits(2);

System.out.println(nf.format(.99));
System.out.println(nf.format(123.567));
System.out.println(nf.format(123.0));

In the above code we set English Locale, and set double format to always have 2 decimal digits. The output is as expected:

0.99
123.57
123.00

Note the dot as separator in formatted number!

References:

  • How to create Java Locale in different ways
  • Check out Java Currency Tutorial
Share with the World!
Categories Java Tags i18n, java, java-util
Previous: Maven run Java app from command line
Next: Merge Sort in Java

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored