The way decimals are displayed varies from location to location. Example: In Germany comma is used instead of a dot to represent a decimal. To format Double in Java, we can make use of DecimalFormat.
- US: 123,456.789
- France: 123 456,789
- Germany: 123.456,789
Example 1: Using Locale
import java.text.DecimalFormat;
import java.util.Locale;
public class FormatDoubleExamples {
public static void main(String... args) {
Double doubleNumber = 40000.1234;
DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.CANADA);
System.out.println(decimalFormat.format(doubleNumber));
decimalFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.GERMANY);
System.out.println("GERMANY: "+ decimalFormat.format(doubleNumber));
decimalFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.FRANCE);
System.out.println("FRANCE: "+ decimalFormat.format(doubleNumber));
decimalFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.JAPAN);
System.out.println("JAPAN: "+ decimalFormat.format(doubleNumber));
}
}
Output:
CANADA: 40,000.123
GERMANY: 40.000,123
FRANCE: 40 000,123
JAPAN: 40,000.123
Example 2: Using String.format
public static void main(String... args) {
Double doubleNumber = 40000.1234;
System.out.println(String.format("%,.3f",doubleNumber));
System.out.println(String.format(Locale.GERMAN,"%,.3f",doubleNumber));
}
Output:
40,000.123
40.000,123
Example 3: Using hash (#) comma (,) and dot (.) patterns:
public static void main(String... args) {
Double myDouble = 12345.67890;
DecimalFormatSymbols germany = new DecimalFormatSymbols(Locale.GERMANY);
DecimalFormatSymbols us = new DecimalFormatSymbols(Locale.US);
DecimalFormat decimalFormat1 = new DecimalFormat("#,###.###",germany);
DecimalFormat decimalFormat2 = new DecimalFormat("#,###.###",us);
System.out.println(decimalFormat1.format(myDouble));
System.out.println(decimalFormat2.format(myDouble));
}
Output:
12.345,679
12,345.679
More Posts related to Java,
- How to Get List of All Country Codes in Java Using Locale Class
- Unsupported major.minor version 52.0 in java
- Java - How to set custom thread name?
- Get the current timestamp in Java
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- java: unclosed string literal [Error]
- Convert Java Byte Array to String with code examples
- Error: Can not find the tag library descriptor for
- Java 8 - Convert List to Map Examples
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Fix java.net.ProtocolException: Invalid HTTP method
- Java: Convert Stream to List
- Java equals method - Tutorial
- List of Java JDBC Database Driver Jars, Classes and URLs Details
- Read YAML file Java Jackson Library
- How to display Java Date Time timezone GMT/UTC offset using SimpleDateFormat
- List of Java Keywords
- Enable JSON Pretty Print in Java Jackson
- How to Word-Warp Console logs in IntelliJ
- Convert Map to List in Java 8 using Stream API
- Create a Directory using Java Code
- Ways to Convert Integer or int to Long in Java
- [Program] How to read three different values using Scanner in Java
- Java JDBC Example with Oracle Database Driver Connection
More Posts:
- Notepad++ Convert text from lower to upper case - NotepadPlusPlus
- How to Compare Strings in Bash - Bash
- Zsh Shell: Custom alias that you may find useful - MacOS
- Amp Hello World Example - AMP
- How to repeat background image in Android Activity - Android
- Java 8 Leap year check using Year class from java.time api - Java
- How to install curl on Alpine Linux - Linux
- Cannot load PowerApps form in SharePoint Online due to repeated authentication - SharePoint