Title: Java DecimalFormat Example - Java Code Geeks
Open Graph Title: Java DecimalFormat Example - Java Code Geeks
Description: In this example, we will see how we can use the Java DecimalFormat class to format decimal numbers. 1. Java DecimalFormat - Introduction  This class is
Open Graph Description: In this example, we will see how we can use the Java DecimalFormat class to format decimal numbers. 1. Java DecimalFormat - Introduction  This class is
Mail addresses
support@javacodegeeks.com
Opengraph URL: https://examples.javacodegeeks.com/java-decimalformat-example/
Generator: WordPress 6.9.4
Domain: examples.javacodegeeks.com
{"@context":"https://schema.org","@graph":[{"@type":"Article","@id":"https://examples.javacodegeeks.com/java-decimalformat-example/#article","isPartOf":{"@id":"https://examples.javacodegeeks.com/java-decimalformat-example/"},"author":{"name":"Anirudh Bhatnagar","@id":"https://examples.javacodegeeks.com/#/schema/person/0779307aef4fa9b0e4050dedba4e0ee8"},"headline":"Java DecimalFormat Example","datePublished":"2014-09-12T12:00:23+00:00","dateModified":"2020-07-31T07:14:49+00:00","mainEntityOfPage":{"@id":"https://examples.javacodegeeks.com/java-decimalformat-example/"},"wordCount":496,"commentCount":0,"publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"image":{"@id":"https://examples.javacodegeeks.com/java-decimalformat-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","articleSection":["DecimalFormat"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https://examples.javacodegeeks.com/java-decimalformat-example/#respond"]}]},{"@type":"WebPage","@id":"https://examples.javacodegeeks.com/java-decimalformat-example/","url":"https://examples.javacodegeeks.com/java-decimalformat-example/","name":"Java DecimalFormat Example - Java Code Geeks","isPartOf":{"@id":"https://examples.javacodegeeks.com/#website"},"primaryImageOfPage":{"@id":"https://examples.javacodegeeks.com/java-decimalformat-example/#primaryimage"},"image":{"@id":"https://examples.javacodegeeks.com/java-decimalformat-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","datePublished":"2014-09-12T12:00:23+00:00","dateModified":"2020-07-31T07:14:49+00:00","description":"In this example, we will see how we can use the Java DecimalFormat class to format decimal numbers. 1. Java DecimalFormat - Introduction  This class is","breadcrumb":{"@id":"https://examples.javacodegeeks.com/java-decimalformat-example/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://examples.javacodegeeks.com/java-decimalformat-example/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/java-decimalformat-example/#primaryimage","url":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https://examples.javacodegeeks.com/java-decimalformat-example/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://examples.javacodegeeks.com/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https://examples.javacodegeeks.com/category/java-development/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https://examples.javacodegeeks.com/category/java-development/core-java/"},{"@type":"ListItem","position":4,"name":"text","item":"https://examples.javacodegeeks.com/category/java-development/core-java/text/"},{"@type":"ListItem","position":5,"name":"DecimalFormat","item":"https://examples.javacodegeeks.com/category/java-development/core-java/text/decimalformat/"},{"@type":"ListItem","position":6,"name":"Java DecimalFormat Example"}]},{"@type":"WebSite","@id":"https://examples.javacodegeeks.com/#website","url":"https://examples.javacodegeeks.com/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://examples.javacodegeeks.com/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https://examples.javacodegeeks.com/#organization","name":"Exelixis Media P.C.","url":"https://examples.javacodegeeks.com/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/logo/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https://examples.javacodegeeks.com/#/schema/logo/image/"},"sameAs":["https://www.facebook.com/javacodegeeks","https://x.com/javacodegeeks"]},{"@type":"Person","@id":"https://examples.javacodegeeks.com/#/schema/person/0779307aef4fa9b0e4050dedba4e0ee8","name":"Anirudh Bhatnagar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/person/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2014/08/Anirudh-Bhatnagar-96x96.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2014/08/Anirudh-Bhatnagar-96x96.jpg","caption":"Anirudh Bhatnagar"},"description":"Anirudh is a Java programmer with extensive experience in building Java/J2EE applications. He has always been fascinated by the new technologies and emerging trends in software development. He has been involved in propagating these changes and new technologies in his projects. He is an avid blogger and agile enthusiast who believes in writing clean and well tested code.","sameAs":["http://www.javacodegeeks.com/"],"url":"https://examples.javacodegeeks.com/author/anirudh-bhatnagar/"}]}
{"@context":"http:\/\/schema.org","@type":"BreadcrumbList","@id":"#Breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"item":{"name":"Home","@id":"https:\/\/examples.javacodegeeks.com\/"}},{"@type":"ListItem","position":2,"item":{"name":"Java Development","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"}},{"@type":"ListItem","position":3,"item":{"name":"Core Java","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"}},{"@type":"ListItem","position":4,"item":{"name":"text","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/text\/"}},{"@type":"ListItem","position":5,"item":{"name":"DecimalFormat","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/text\/decimalformat\/"}}]}
{"@context":"http:\/\/schema.org","@type":"Article","dateCreated":"2014-09-12T15:00:23+03:00","datePublished":"2014-09-12T15:00:23+03:00","dateModified":"2020-07-31T10:14:49+03:00","headline":"Java DecimalFormat Example","name":"Java DecimalFormat Example","keywords":[],"url":"https:\/\/examples.javacodegeeks.com\/java-decimalformat-example\/","description":"In this example, we will see how we can use the Java DecimalFormat class to format decimal numbers. 1. Java DecimalFormat - Introduction \u00c2 This class is useful when we want to format the decimal numbe","copyrightYear":"2014","articleSection":"DecimalFormat","articleBody":"\nIn this example, we will see how we can use the Java DecimalFormat class to format decimal numbers.\n\n\n\n1. Java DecimalFormat - Introduction\n\n\n\n\u00c2 This class is useful when we want to format the decimal numbers in the following ways:\n\n\n\n\n\n\n\nAs per a defined specific string pattern: We can specify a pattern string to define the display of the decimal.\n\n\n\nSpecific to a locale: We can use locale-specific settings to control the application of patterns for the display of decimals.\n\n\n\n2. Technologies used\n\n\n\nThe example code in this article was built and run using:\n\n\n\nJava 1.8.231(1.8.x will do fine)Eclipse IDE for Enterprise Java Developers-Photon\n\n\n\n3. Available characters for the pattern\n\n\n\nHere you can see the available character that you can use inside the patterns.\n\n\n\nSymbolMeaning0Digit#Digit, zero shows as absent.Decimal separator%Multiply by 100 and show as a percentage,Grouping separator;Separates negative and positive subpatterns\\Multiply by 1000 and show as per mille value\u00a4Currency sign, replaced by currency symbol. If doubled, replaced by the international currency symbol. 'Used to quote special characters in a prefix or suffix.ESeparates mantissa and exponent in scientific notation.-Minus signAvailable characters table\n\n\n\n4. Using a decimal format java pattern string\n\n\n\n \nimport java.text.DecimalFormat;\n \npublic class JavaDecimalFormatExample {\n \n private static final String COMMA_SEPERATED = "###,###.###";\n private static double number = 12345.6;\n \n public static void main(String[] args) {\n \n DecimalFormat decimalFormat = new DecimalFormat(COMMA_SEPERATED);\n System.out.println(decimalFormat.format(number));\n }\n}\n\n\n\nOutput\n\n\n\n\n\n\n\n\n\n1\n\n\n\n12,345.6\n\n\n\n\n\n\n\n\n\n\nIn the above example we have defined a String PATERN which is passed as a parameter to the constructor of class DecimalFormat. Then we used the method format, in which we passed the decimal and it returned the desired formatted decimal string.\n\n\n\nWe can change this pattern later by using the method applyPattern().Lets suppose we want show the number with two decimal places. Lets see an example of how to do it:\n\n\n\n\n\n\n\n\n\n1\n2\n3\n4\n5\n\n\n\n...\nprivate static final String TWO_DECIMAL_PLACES_WITH_COMMA = \"###,###.00\";\n System.out.println(\"After another pattern\");\n decimalFormat.applyPattern(TWO_DECIMAL_PLACES_WITH_COMMA);\n System.out.println(decimalFormat.format(number));\n\n\n\n\n\n\n\n\n\n\nOutput\n\n\n\n\n\n\n\n\n\n1\n2\n\n\n\nAfter another pattern\n12,345.60\n\n\n\n\n\n\n\n\n\n\nSo, different patterns can be used to achieve different patterns, below are a few examples:\n\n\n\nimport java.text.DecimalFormat;\n \npublic class JavaDecimalFormatExample {\n private static final String FIXED_PLACES = "000,000.00";\n private static final String BEGIN_WITH_DOLLAR = "$###,###.00";\n \n private static double number = 12345.6;\n \n public static void main(String[] args) {\n \n applyCustomFormat(BEGIN_WITH_DOLLAR, number);\n applyCustomFormat(FIXED_PLACES, number);\n }\n \n private static void applyCustomFormat(String pattern, double value) {\n DecimalFormat decimalFormat = new DecimalFormat(pattern);\n decimalFormat.applyPattern(pattern);\n System.out.println(decimalFormat.format(value));\n }\n}\n\n\n\nOutput\n\n\n\n\n\n\n\n\n\n1\n2\n\n\n\n$12,345.60\n012,345.60\n\n\n\n\n\n\n\n\n\n\n5. Using locale-specific pattern\n\n\n\nIn the previous examples we created a DecimalFormat for the default Locale of the JVM, where the code is running. In case we want to display the numbers as per the locale, we would create an object of the class NumberFormat passing it the locale and then cast it to DecimalFormat.\n\n\n\nLets see an example:\n\n\n\n\n\n\n\n\n\n1\n2\n3\n4\n5\n\n\n\n.....\n Locale UK_LOCALE = new Locale(\"en\", \"UK\");\n Locale US_LOCALE = new Locale(\"en\",\"US\");\n applyCustomFormat(COMMA_SEPERATED, number, UK_LOCALE);\n applyCustomFormat(BEGIN_WITH_DOLLAR, number, US_LOCALE);\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n1\n2\n3\n4\n5\n\n\n\nprivate static void applyCustomFormat(String pattern, double value, Locale locale) {\n DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(locale);\n System.out.println(decimalFormat.format(value));\n \n }\n\n\n\n\n\n\n\n\n\n\nOutput\n\n\n\n\n\n\n\n\n\n1\n2\n\n\n\n12,345.6\n12,345.6\n\n\n\n\n\n\n\n\n\n\nHere, the system locale is set to UK so the output is been formatted as per the UK locale.\n\n\n\nThere are few other methods and scenarios which can be useful, lets see them :\n\n\n\n5.1 Grouping\n\n\n\nWe can group the digits using the method setGroupingSize(integer). This method groups the numbers starting from the decimal.\n\n\n\nLets see an example:\n\n\n\n\n\n\n\n\n\n1\n2\n3\n4\n5\n6\n\n\n\n ...\n \/\/setting group\nDecimalFormat groupeddecimalFormat = new DecimalFormat(\"###,###.###\");\ngroupeddecimalFormat.setGroupingSize(4);\nSystem.out.println(groupeddecimalFormat.format(13243534.32));\n ....\n\n\n\n\n\n\n\n\n\n\nOutput\n\n\n\n\n\n\n\n\n\n1\n\n\n\n13,243,534.32\n\n\n\n\n\n\n\n\n\n\nIn the above example we can see that the large number is grouped into 4 integers, also observe that the pattern applied is overwritten by the group set method.\n\n\n\n6. Example for DecimalFormatSymbols\n\n\n\nDecimalFormatSymbols is an object from the DecimalFormat class that is used to change any of the available symbols that we use for patterns to another symbol if needed.\n\n\n\nHere you can see an example about DecimalFormatSymbols.\n\n\n\nimport java.text.DecimalFormatSymbols;\npublic class DFSymbols {\n\t public static void main(String[] args) { \n\t DecimalFormatSymbols dfsymbol=new DecimalFormatSymbols(); \n\t System.out.println("Decimal separator current character:" + dfsymbol.getDecimalSeparator()); \n\t \n\t char decSep = '*'; \n\t \n\t dfsymbol.setDecimalSeparator(decSep); \n\t System.out.println("The updated character:"+ dfsymbol.getDecimalSeparator()); \n\t } \n}\n\n\n\n\nOutput\n\n\n\nDecimal separator current character:.\nThe updated character:*\n\n\n\n\n7. Download the source code\n\n\n\nDownloadYou can download the full source code of this example here: Java DecimalFormat Example\n\n\n\nLast updated on Apr. 23rd, 2020\n","publisher":{"@id":"#Publisher","@type":"Organization","name":"Examples Java Code Geeks","logo":{"@type":"ImageObject","url":"\/wp-content\/uploads\/2013\/05\/JavaCodeGeeks-logo.png"},"sameAs":["https:\/\/feeds.feedburner.com\/examplesjavacodegeeks","https:\/\/www.facebook.com\/javacodegeeks","https:\/\/twitter.com\/javacodegeeks","https:\/\/www.linkedin.com\/groups\/3810709\/","https:\/\/www.youtube.com\/channel\/UCxoUc7Rar2q90Gu0nT2ffuQ","https:\/\/github.com\/javacodegeeks\/"]},"sourceOrganization":{"@id":"#Publisher"},"copyrightHolder":{"@id":"#Publisher"},"mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-decimalformat-example\/","breadcrumb":{"@id":"#Breadcrumb"}},"author":{"@type":"Person","name":"Anirudh Bhatnagar","url":"https:\/\/examples.javacodegeeks.com\/author\/anirudh-bhatnagar\/"},"image":{"@type":"ImageObject","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":1200,"height":150}}
| og:locale | en_US |
| og:type | article |
| og:site_name | Examples Java Code Geeks |
| article:publisher | https://www.facebook.com/javacodegeeks |
| article:published_time | 2014-09-12T12:00:23+00:00 |
| article:modified_time | 2020-07-31T07:14:49+00:00 |
| og:image | https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg |
| og:image:width | 150 |
| og:image:height | 150 |
| og:image:type | image/jpeg |
| author | Anirudh Bhatnagar |
| twitter:card | summary_large_image |
| twitter:creator | @javacodegeeks |
| twitter:label1 | Written by |
| twitter:data1 | Anirudh Bhatnagar |
| twitter:label2 | Est. reading time |
| twitter:data2 | 4 minutes |
| None | IE=edge |
| msapplication-TileImage | https://examples.javacodegeeks.com/wp-content/uploads/2013/05/JavaCodeGeeks-favIcon.png |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1