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,
- [Fix] java.time.zone.ZoneRulesException: Unknown time-zone ID
- Parse XML file in Java using DOM Parser
- Java equals method - Tutorial
- [Program] How to read three different values using Scanner in Java
- Java: The value of the local variable string is not used
- Display Output in Java Console as a Table
- How to detect Operating System using Java code
- Java 8 Streams map() with examples
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Add newline character Java code example (\r \n \r\n)
- List of Java Major Minor Version Numbers
- IntelliJ Keyboard Shortcut to remove unused imports [Java]
- Java - Check if array contains the value
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- How to declare and initialize Array in Java Programming
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- XmlRpcException ConnectException connection refused error
- Create a Zip file using Java Code programmatically
- List of jar files for Jax-ws (SOAP) based Java Web Services
- How to fix Java HTTP java.net.UnknownHostException
- List of jars required for Struts2 project
- [fix] java: incompatible types: double cannot be converted to java.lang.Integer Generics
- Maven BUILD FAILURE: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin
- Get the current timestamp in Java
- java: unclosed string literal [Error]
More Posts:
- How to know if you have blocked your friend Number on Android Phone - Android
- Enabling Notepad++ Dark Theme - NotepadPlusPlus
- Create SharePoint Site Collection with new Content database in existing web application - SharePoint
- How to do a Reverse Image Search Using Google Tools - Google
- [jQuery] Uncaught ReferenceError: $ is not defined at index.html:5 - jQuery
- SQLite Error: unknown command or invalid arguments: open. Enter .help for help - Android
- See actual SharePoint error exception modify web.config - SharePoint
- How to use Content Assist in Eclipse IDE - Eclipse