A logo showing the text blog.marcnuri.com
Español
Home»Java»Numbers to Strings with custom symbols // DecimalFormat - DecimalFormatSymbols

Recent Posts

  • Fabric8 Kubernetes Client 6.4.0 is now available!
  • I bought an iPad
  • Three years at Red Hat
  • Fabric8 Kubernetes Client 6.3.1 is now available!
  • Eclipse JKube 1.10 is now available!

Categories

  • Front-end
  • Java
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Tools

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • December 2019
  • October 2019
  • September 2019
  • July 2019
  • March 2019
  • November 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • July 2017
  • December 2015
  • November 2015
  • November 2008
  • November 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

Numbers to Strings with custom symbols // DecimalFormat - DecimalFormatSymbols

2007-05-07 in Java / Legacy tagged DecimalFormat / Java / Numbers by Marc Nuri | Last updated: 2021-02-06

When printing reports or casting Strings to Numbers, it's very useful to take advantage of the DecimalFormat class found in the java.text package.

This class is pretty straightforward and easy to use, but some problems may be found when your work with this class in computers from other countries. Internationalization will cause your program to behave in a way you may not expect in some cases.

If you use this class to parse a number to a String, you may find that when a user enters a number in a french computer you will be getting a ParseException, because the computer is expecting different symbols. Here is where the DecimalFormatSymbols class becomes very useful.

With the following code, I'll show you how easy is to change the symbols and make them independent of different Locales.

1// We create our formatter using a DecimalFormat Instance.
2// We use this method instead of (new DecimalFormat(String pattern))
3// because in computers with different locales, we may have problems when
4// setting the pattern. This way we assure we always work with the same variables.
5DecimalFormat format = (DecimalFormat)DecimalFormat.getInstance(Locale.ENGLISH);
6// We set the typical pattern with thousand separator and two decimal places
7format.applyPattern("#,##0.00");
8// Now we do the work to change the formatting symbols
9// We will set the typical spanish symbols
10DecimalFormatSymbols dfs = format.getDecimalFormatSymbols();
11dfs.setDecimalSeparator(',');
12dfs.setGroupingSeparator('.');
13format.setDecimalFormatSymbols(dfs);
14// We do two simple tests
15System.out.println(format.format(1000234234.56772345d));
16try {
17  System.out.println(format.parse("1.258.254,25"));
18} catch (ParseException ex) {
19  ex.printStackTrace();
20}
Twitter iconFacebook iconLinkedIn iconPinterest iconEmail icon

Post navigation

← SimpleDateFormat to check user date input // parsing String to dates in javaJava recursive functions explained // Using recursion to sum an array of numbers →
© 2007 - 2023 Marc Nuri