From: Tuukka Lehtonen Date: Tue, 3 Jan 2017 08:35:48 +0000 (+0200) Subject: Allow specification of Locale to FormattingUtils.significantDigitFormat X-Git-Tag: v1.27.0~42 X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=988ec1a7ac7d5e6a354601635f2676935809d93c;hp=a7eaa6a719acf77b115f48aeaab901ebedaf0a9b Allow specification of Locale to FormattingUtils.significantDigitFormat refs #6929 Change-Id: I67b9192ebe8113e44fdbee6d089dbef58b59bc39 --- diff --git a/bundles/org.simantics.utils/src/org/simantics/utils/format/FormattingUtils.java b/bundles/org.simantics.utils/src/org/simantics/utils/format/FormattingUtils.java index 11d37e305..be31244da 100644 --- a/bundles/org.simantics.utils/src/org/simantics/utils/format/FormattingUtils.java +++ b/bundles/org.simantics.utils/src/org/simantics/utils/format/FormattingUtils.java @@ -34,10 +34,18 @@ public class FormattingUtils { } public static Format significantDigitFormat(int withSignificantDigits) { - return significantDigitFormat(withSignificantDigits, false); + return significantDigitFormat(Locale.getDefault(Locale.Category.FORMAT), withSignificantDigits); + } + + public static Format significantDigitFormat(Locale locale, int withSignificantDigits) { + return significantDigitFormat(locale, withSignificantDigits, false); } public static Format significantDigitFormat(int withSignificantDigits, boolean stripTrailingZeros) { + return significantDigitFormat(Locale.getDefault(Locale.Category.FORMAT), withSignificantDigits, stripTrailingZeros); + } + + public static Format significantDigitFormat(Locale locale, int withSignificantDigits, boolean stripTrailingZeros) { if (withSignificantDigits < 1) throw new IllegalArgumentException("withSignificantDigits must be > 0, got " + withSignificantDigits); StringBuilder sb = new StringBuilder(withSignificantDigits + 3); @@ -45,9 +53,9 @@ public class FormattingUtils { for (int i = 0; i < withSignificantDigits-1; i++) sb.append("#"); sb.append("E0"); - NumberFormat low = new DecimalFormat(sb.toString()); + NumberFormat low = new DecimalFormat(sb.toString(), DecimalFormatSymbols.getInstance(locale)); low.setGroupingUsed(false); - NumberFormat hi = new SignificantDigitFormat(withSignificantDigits, stripTrailingZeros); + NumberFormat hi = new SignificantDigitFormat(locale, withSignificantDigits, stripTrailingZeros); Format format = new SwitchFormat(0.1, 1, low, hi, hi); return format; } @@ -61,10 +69,10 @@ public class FormattingUtils { private boolean fixDecimalSeparator; private char decimalSeparator; - public SignificantDigitFormat(int digits, boolean stripTrailingZeros) { + public SignificantDigitFormat(Locale locale, int digits, boolean stripTrailingZeros) { this.mc = new MathContext(digits); this.stripTrailingZeros = stripTrailingZeros; - DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(); + DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale); decimalSeparator = symbols.getDecimalSeparator(); // BigDecimal always formats doubles with '.' as decimal separator. if (decimalSeparator != '.')