/** * Returns the value as a 0.12 type string. More digits after the decimal place will be used * if necessary, but two will always be present. */ public String toFriendlyString() { return type.getMonetaryFormat().format(this).toString(); }
/** * Format the given monetary value to a human readable form. */ public CharSequence format(Monetary monetary) { return format(monetary, monetary.smallestUnitExponent()); }
/** * <p> * Returns the value as a plain string denominated in BTC. * The result is unformatted with no trailing zeroes. * For instance, a value of 150000 satoshis gives an output string of "0.0015" BTC * </p> */ public String toPlainString() { return type.getPlainFormat().format(this).toString(); }
private static CharSequence format(final MonetaryFormat format, final boolean signed, final Monetary value) { if (value == null) return ""; checkArgument(value.signum() >= 0 || signed); int smallestUnitExponent = value.smallestUnitExponent(); if (signed) return format.negativeSign(Constants.CURRENCY_MINUS_SIGN).positiveSign(Constants.CURRENCY_PLUS_SIGN).format(value, smallestUnitExponent); else return format.format(value, smallestUnitExponent); }
@Test public void testSigns() throws Exception { assertEquals("-1.00", NO_CODE.format(Coin.COIN.negate()).toString()); assertEquals("@1.00", NO_CODE.negativeSign('@').format(Coin.COIN.negate()).toString()); assertEquals("1.00", NO_CODE.format(Coin.COIN).toString()); assertEquals("+1.00", NO_CODE.positiveSign('+').format(Coin.COIN).toString()); }
private String formatRepeat(Coin coin, int decimals, int repetitions) { return NO_CODE.minDecimals(0).repeatOptionalDecimals(decimals, repetitions).format(coin).toString(); }
@Test public void withLocale() throws Exception { final Coin value = Coin.valueOf(-1234567890l); assertEquals("-12.34567890", NO_CODE.withLocale(Locale.US).format(value).toString()); assertEquals("-12,34567890", NO_CODE.withLocale(Locale.GERMANY).format(value).toString()); assertEquals("-१२.३४५६७८९०", NO_CODE.withLocale(new Locale("hi", "IN")).format(value).toString()); // Devanagari }
@Test(expected = NumberFormatException.class) public void missingCode() throws Exception { MonetaryFormat.UBTC.shift(1).format(Coin.ZERO); }
@Test public void codeSeparator() throws Exception { assertEquals("BTC@0.00", MonetaryFormat.BTC.codeSeparator('@').format(Coin.ZERO).toString()); }
private String format(Coin coin, int shift, int minDecimals, int... decimalGroups) { return NO_CODE.shift(shift).minDecimals(minDecimals).optionalDecimals(decimalGroups).format(coin).toString(); }
@Test public void testDigits() throws Exception { assertEquals("١٢.٣٤٥٦٧٨٩٠", NO_CODE.digits('\u0660').format(Coin.valueOf(1234567890l)).toString()); }
@Test public void customCode() throws Exception { assertEquals("dBTC 0", MonetaryFormat.UBTC.code(1, "dBTC").shift(1).format(Coin.ZERO).toString()); }