avoid using locale for unit formatting

This commit is contained in:
Craig Raw 2022-11-02 11:40:39 +02:00
parent 402d9b14ec
commit 6481d83b0c
2 changed files with 31 additions and 17 deletions

View file

@ -8,15 +8,20 @@ import java.util.Locale;
public enum UnitFormat { public enum UnitFormat {
DOT { DOT {
private final DecimalFormat btcFormat = new DecimalFormat("0", DecimalFormatSymbols.getInstance(getLocale())); private final DecimalFormat btcFormat = new DecimalFormat("0", getDecimalFormatSymbols());
private final DecimalFormat tableBtcFormat = new DecimalFormat("0.00000000", DecimalFormatSymbols.getInstance(getLocale())); private final DecimalFormat satsFormat = new DecimalFormat("#,##0", getDecimalFormatSymbols());
private final DecimalFormat currencyFormat = new DecimalFormat("#,##0.00", DecimalFormatSymbols.getInstance(getLocale())); private final DecimalFormat tableBtcFormat = new DecimalFormat("0.00000000", getDecimalFormatSymbols());
private final DecimalFormat currencyFormat = new DecimalFormat("#,##0.00", getDecimalFormatSymbols());
public DecimalFormat getBtcFormat() { public DecimalFormat getBtcFormat() {
btcFormat.setMaximumFractionDigits(8); btcFormat.setMaximumFractionDigits(8);
return btcFormat; return btcFormat;
} }
public DecimalFormat getSatsFormat() {
return satsFormat;
}
public DecimalFormat getTableBtcFormat() { public DecimalFormat getTableBtcFormat() {
return tableBtcFormat; return tableBtcFormat;
} }
@ -25,20 +30,28 @@ public enum UnitFormat {
return currencyFormat; return currencyFormat;
} }
public Locale getLocale() { public DecimalFormatSymbols getDecimalFormatSymbols() {
return Locale.ENGLISH; DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
symbols.setGroupingSeparator(',');
return symbols;
} }
}, },
COMMA { COMMA {
private final DecimalFormat btcFormat = new DecimalFormat("0", DecimalFormatSymbols.getInstance(getLocale())); private final DecimalFormat btcFormat = new DecimalFormat("0", getDecimalFormatSymbols());
private final DecimalFormat tableBtcFormat = new DecimalFormat("0.00000000", DecimalFormatSymbols.getInstance(getLocale())); private final DecimalFormat satsFormat = new DecimalFormat("#,##0", getDecimalFormatSymbols());
private final DecimalFormat currencyFormat = new DecimalFormat("#,##0.00", DecimalFormatSymbols.getInstance(getLocale())); private final DecimalFormat tableBtcFormat = new DecimalFormat("0.00000000", getDecimalFormatSymbols());
private final DecimalFormat currencyFormat = new DecimalFormat("#,##0.00", getDecimalFormatSymbols());
public DecimalFormat getBtcFormat() { public DecimalFormat getBtcFormat() {
btcFormat.setMaximumFractionDigits(8); btcFormat.setMaximumFractionDigits(8);
return btcFormat; return btcFormat;
} }
public DecimalFormat getSatsFormat() {
return satsFormat;
}
public DecimalFormat getTableBtcFormat() { public DecimalFormat getTableBtcFormat() {
return tableBtcFormat; return tableBtcFormat;
} }
@ -47,15 +60,20 @@ public enum UnitFormat {
return currencyFormat; return currencyFormat;
} }
public Locale getLocale() { public DecimalFormatSymbols getDecimalFormatSymbols() {
return Locale.GERMAN; 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 getBtcFormat();
public abstract DecimalFormat getSatsFormat();
public abstract DecimalFormat getTableBtcFormat(); public abstract DecimalFormat getTableBtcFormat();
public abstract DecimalFormat getCurrencyFormat(); public abstract DecimalFormat getCurrencyFormat();
@ -65,17 +83,13 @@ public enum UnitFormat {
} }
public String formatSatsValue(Long amount) { public String formatSatsValue(Long amount) {
return String.format(getLocale(), "%,d", amount); return getSatsFormat().format(amount);
} }
public String formatCurrencyValue(double amount) { public String formatCurrencyValue(double amount) {
return getCurrencyFormat().format(amount); return getCurrencyFormat().format(amount);
} }
public DecimalFormatSymbols getDecimalFormatSymbols() {
return DecimalFormatSymbols.getInstance(getLocale());
}
public String getGroupingSeparator() { public String getGroupingSeparator() {
return Character.toString(getDecimalFormatSymbols().getGroupingSeparator()); return Character.toString(getDecimalFormatSymbols().getGroupingSeparator());
} }

View file

@ -30,7 +30,7 @@ public class CoinTextFormatter extends TextFormatter<String> {
public CoinFilter(UnitFormat unitFormat) { public CoinFilter(UnitFormat unitFormat) {
this.unitFormat = unitFormat == null ? UnitFormat.DOT : 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})?"); this.coinValidation = Pattern.compile("[\\d" + Pattern.quote(unitFormat.getGroupingSeparator()) + "]*(" + Pattern.quote(unitFormat.getDecimalSeparator()) + "\\d{0,8})?");
} }