From 6481d83b0c13a6a134a9a1b8033ecec95a6c95f2 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 2 Nov 2022 11:40:39 +0200 Subject: [PATCH] avoid using locale for unit formatting --- .../com/sparrowwallet/sparrow/UnitFormat.java | 46 ++++++++++++------- .../sparrow/control/CoinTextFormatter.java | 2 +- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/UnitFormat.java b/src/main/java/com/sparrowwallet/sparrow/UnitFormat.java index 2be8c257..41a1afb2 100644 --- a/src/main/java/com/sparrowwallet/sparrow/UnitFormat.java +++ b/src/main/java/com/sparrowwallet/sparrow/UnitFormat.java @@ -8,15 +8,20 @@ import java.util.Locale; public enum UnitFormat { DOT { - private final DecimalFormat btcFormat = new DecimalFormat("0", DecimalFormatSymbols.getInstance(getLocale())); - private final DecimalFormat tableBtcFormat = new DecimalFormat("0.00000000", DecimalFormatSymbols.getInstance(getLocale())); - private final DecimalFormat currencyFormat = new DecimalFormat("#,##0.00", DecimalFormatSymbols.getInstance(getLocale())); + private final DecimalFormat btcFormat = new DecimalFormat("0", getDecimalFormatSymbols()); + private final DecimalFormat satsFormat = new DecimalFormat("#,##0", getDecimalFormatSymbols()); + private final DecimalFormat tableBtcFormat = new DecimalFormat("0.00000000", getDecimalFormatSymbols()); + private final DecimalFormat currencyFormat = new DecimalFormat("#,##0.00", getDecimalFormatSymbols()); public DecimalFormat getBtcFormat() { btcFormat.setMaximumFractionDigits(8); return btcFormat; } + public DecimalFormat getSatsFormat() { + return satsFormat; + } + public DecimalFormat getTableBtcFormat() { return tableBtcFormat; } @@ -25,20 +30,28 @@ public enum UnitFormat { return currencyFormat; } - public Locale getLocale() { - return Locale.ENGLISH; + public DecimalFormatSymbols getDecimalFormatSymbols() { + DecimalFormatSymbols symbols = new DecimalFormatSymbols(); + symbols.setDecimalSeparator('.'); + symbols.setGroupingSeparator(','); + return symbols; } }, COMMA { - private final DecimalFormat btcFormat = new DecimalFormat("0", DecimalFormatSymbols.getInstance(getLocale())); - private final DecimalFormat tableBtcFormat = new DecimalFormat("0.00000000", DecimalFormatSymbols.getInstance(getLocale())); - private final DecimalFormat currencyFormat = new DecimalFormat("#,##0.00", DecimalFormatSymbols.getInstance(getLocale())); + private final DecimalFormat btcFormat = new DecimalFormat("0", getDecimalFormatSymbols()); + private final DecimalFormat satsFormat = new DecimalFormat("#,##0", getDecimalFormatSymbols()); + private final DecimalFormat tableBtcFormat = new DecimalFormat("0.00000000", getDecimalFormatSymbols()); + private final DecimalFormat currencyFormat = new DecimalFormat("#,##0.00", getDecimalFormatSymbols()); public DecimalFormat getBtcFormat() { btcFormat.setMaximumFractionDigits(8); return btcFormat; } + public DecimalFormat getSatsFormat() { + return satsFormat; + } + public DecimalFormat getTableBtcFormat() { return tableBtcFormat; } @@ -47,15 +60,20 @@ public enum UnitFormat { return currencyFormat; } - public Locale getLocale() { - return Locale.GERMAN; + public DecimalFormatSymbols getDecimalFormatSymbols() { + DecimalFormatSymbols symbols = new DecimalFormatSymbols(); + symbols.setDecimalSeparator(','); + symbols.setGroupingSeparator('.'); + return symbols; } }; - public abstract Locale getLocale(); + public abstract DecimalFormatSymbols getDecimalFormatSymbols(); public abstract DecimalFormat getBtcFormat(); + public abstract DecimalFormat getSatsFormat(); + public abstract DecimalFormat getTableBtcFormat(); public abstract DecimalFormat getCurrencyFormat(); @@ -65,17 +83,13 @@ public enum UnitFormat { } public String formatSatsValue(Long amount) { - return String.format(getLocale(), "%,d", amount); + return getSatsFormat().format(amount); } public String formatCurrencyValue(double amount) { return getCurrencyFormat().format(amount); } - public DecimalFormatSymbols getDecimalFormatSymbols() { - return DecimalFormatSymbols.getInstance(getLocale()); - } - public String getGroupingSeparator() { return Character.toString(getDecimalFormatSymbols().getGroupingSeparator()); } diff --git a/src/main/java/com/sparrowwallet/sparrow/control/CoinTextFormatter.java b/src/main/java/com/sparrowwallet/sparrow/control/CoinTextFormatter.java index 9e32b87b..e19be1d6 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/CoinTextFormatter.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/CoinTextFormatter.java @@ -30,7 +30,7 @@ public class CoinTextFormatter extends TextFormatter { public CoinFilter(UnitFormat unitFormat) { this.unitFormat = unitFormat == null ? UnitFormat.DOT : unitFormat; - this.coinFormat = new DecimalFormat("###,###.########", DecimalFormatSymbols.getInstance(unitFormat.getLocale())); + this.coinFormat = new DecimalFormat("###,###.########", unitFormat.getDecimalFormatSymbols()); this.coinValidation = Pattern.compile("[\\d" + Pattern.quote(unitFormat.getGroupingSeparator()) + "]*(" + Pattern.quote(unitFormat.getDecimalSeparator()) + "\\d{0,8})?"); }