source: josm/trunk/src/org/openstreetmap/josm/tools/JosmDecimalFormatSymbolsProvider.java@ 13111

Last change on this file since 13111 was 13104, checked in by Don-vip, 6 years ago

fix some recent Sonar issues

  • Property svn:eol-style set to native
File size: 2.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.text.DecimalFormat;
5import java.text.DecimalFormatSymbols;
6import java.text.NumberFormat;
7import java.text.spi.DecimalFormatSymbolsProvider;
8import java.util.Locale;
9
10/**
11 * JOSM implementation of the {@link java.text.DecimalFormatSymbols DecimalFormatSymbols} class,
12 * consistent with ISO 80000-1.
13 * This class will only be used with Java 9 and later runtimes, as Java 8 implementation relies
14 * on Java Extension Mechanism only, while Java 9 supports application classpath.
15 * See {@link java.util.spi.LocaleServiceProvider LocaleServiceProvider} javadoc for more details.
16 * @since 12931
17 */
18public class JosmDecimalFormatSymbolsProvider extends DecimalFormatSymbolsProvider {
19
20 @Override
21 public DecimalFormatSymbols getInstance(Locale locale) {
22 DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
23 // Override digit group separator to be consistent across languages with ISO 80000-1, chapter 7.3.1
24 symbols.setGroupingSeparator('\u202F'); // U+202F: NARROW NO-BREAK SPACE
25 return symbols;
26 }
27
28 @Override
29 public Locale[] getAvailableLocales() {
30 return I18n.getAvailableTranslations();
31 }
32
33 /**
34 * Returns a new {@code double} initialized to the value represented by the specified {@code String},
35 * allowing both dot and comma decimal separators.
36 *
37 * @param s the string to be parsed.
38 * @return the {@code double} value represented by the string argument.
39 * @throws NullPointerException if the string is null
40 * @throws NumberFormatException if the string does not contain a parsable {@code double}.
41 * @see Double#parseDouble(String)
42 * @since 13050
43 */
44 public static double parseDouble(String s) {
45 String text = s;
46 NumberFormat format = DecimalFormat.getInstance();
47 if (format instanceof DecimalFormat) {
48 char decimalSeparator = ((DecimalFormat) format).getDecimalFormatSymbols().getDecimalSeparator();
49 text = text.replace('.', decimalSeparator).replace(',', decimalSeparator);
50 }
51 return Double.parseDouble(text);
52 }
53}
Note: See TracBrowser for help on using the repository browser.