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,
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
More Posts:
- How to Run PowerShell Script (.ps1) on Mac - MacOS
- How to migrate SharePoint Designer 2010 workflow to Power Automate FLOW (Microsoft Office 365) - SharePoint
- Using Java 8 Predicate with 2 Arguments Example - Java
- Microsoft Teams Error code - 6 issue - Teams
- How to connect Relational and NoSQL databases with Spring Boot - HowTos
- How to get file path of a file using Java Code? - Java
- Add Custom External Link to SharePoint Site Navigation - SharePoint
- YAML Parser using Java Jackson Library Example - Java