MonetaryFormat: Add default format based on the Bitcoin symbol supported by Unicode 10.0.

This commit is contained in:
Andreas Schildbach 2019-02-27 18:41:04 +01:00
parent 4daf6de1eb
commit 21a32b5604
2 changed files with 18 additions and 3 deletions

View file

@ -60,6 +60,12 @@ public final class MonetaryFormat {
public static final String CODE_MBTC = "mBTC";
/** Currency code for base 1/1000000 Bitcoin. */
public static final String CODE_UBTC = "µBTC";
/** Currency symbol for base 1 Bitcoin. */
public static final String SYMBOL_BTC = "\u20bf";
/** Currency symbol for base 1/1000 Bitcoin. */
public static final String SYMBOL_MBTC = "m" + SYMBOL_BTC;
/** Currency symbol for base 1/1000000 Bitcoin. */
public static final String SYMBOL_UBTC = "µ" + SYMBOL_BTC;
public static final int MAX_DECIMALS = 8;
@ -292,6 +298,10 @@ public final class MonetaryFormat {
}
public MonetaryFormat() {
this(false);
}
public MonetaryFormat(boolean useBitcoinSymbol) {
// defaults
this.negativeSign = '-';
this.positiveSign = 0; // none
@ -302,9 +312,9 @@ public final class MonetaryFormat {
this.shift = 0;
this.roundingMode = RoundingMode.HALF_UP;
this.codes = new String[MAX_DECIMALS];
this.codes[0] = CODE_BTC;
this.codes[3] = CODE_MBTC;
this.codes[6] = CODE_UBTC;
this.codes[0] = useBitcoinSymbol ? SYMBOL_BTC : CODE_BTC;
this.codes[3] = useBitcoinSymbol ? SYMBOL_MBTC : CODE_MBTC;
this.codes[6] = useBitcoinSymbol ? SYMBOL_UBTC : CODE_UBTC;
this.codeSeparator = ' ';
this.codePrefixed = true;
}

View file

@ -216,6 +216,11 @@ public class MonetaryFormatTest {
assertEquals("µBTC 0", MonetaryFormat.UBTC.format(Coin.ZERO).toString());
}
@Test
public void standardSymbol() throws Exception {
assertEquals(MonetaryFormat.SYMBOL_BTC + " 0.00", new MonetaryFormat(true).format(Coin.ZERO).toString());
}
@Test
public void customCode() throws Exception {
assertEquals("dBTC 0", MonetaryFormat.UBTC.code(1, "dBTC").shift(1).format(Coin.ZERO).toString());