/** * Set character range to use for representing digits. It starts with the specified character representing zero. */ public MonetaryFormat digits(char zeroDigit) { if (zeroDigit == this.zeroDigit) return this; else return new MonetaryFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift, roundingMode, codes, codeSeparator, codePrefixed); }
/** * <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 PLAIN_FORMAT.format(this).toString(); }
/** * Test clearing all codes, and then setting codes after clearing. */ @Test public void noCode() throws Exception { assertEquals("0", MonetaryFormat.UBTC.noCode().shift(0).format(Coin.ZERO).toString()); // Ensure that inserting a code after codes are wiped, works assertEquals("dBTC 0", MonetaryFormat.UBTC.noCode().code(1, "dBTC").shift(1).format(Coin.ZERO).toString()); }
/** * Parse a human readable fiat value to a {@link org.bitcoinj.utils.Fiat} instance. * * @throws NumberFormatException * if the string cannot be parsed for some reason */ public Fiat parseFiat(String currencyCode, String str) throws NumberFormatException { return Fiat.valueOf(currencyCode, parseValue(str, Fiat.SMALLEST_UNIT_EXPONENT)); }
private String format(Coin coin, int shift, int minDecimals, int... decimalGroups) { return NO_CODE.shift(shift).minDecimals(minDecimals).optionalDecimals(decimalGroups).format(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 codeOrientation() throws Exception { assertEquals("BTC 0.00", MonetaryFormat.BTC.prefixCode().format(Coin.ZERO).toString()); assertEquals("0.00 BTC", MonetaryFormat.BTC.postfixCode().format(Coin.ZERO).toString()); }
@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()); }
@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 public void testDecimalMark() throws Exception { assertEquals("1.00", NO_CODE.format(Coin.COIN).toString()); assertEquals("1,00", NO_CODE.decimalMark(',').format(Coin.COIN).toString()); }
@Test public void codeSeparator() throws Exception { assertEquals("BTC@0.00", MonetaryFormat.BTC.codeSeparator('@').format(Coin.ZERO).toString()); }
@Test public void parse() throws Exception { assertEquals(Coin.COIN, NO_CODE.parse("1")); assertEquals(Coin.COIN, NO_CODE.parse("1.")); assertEquals(Coin.COIN, NO_CODE.parse("1.0")); assertEquals(Coin.COIN, NO_CODE.decimalMark(',').parse("1,0")); assertEquals(Coin.COIN, NO_CODE.parse("01.0000000000")); assertEquals(Coin.COIN, NO_CODE.positiveSign('+').parse("+1.0")); assertEquals(Coin.COIN.negate(), NO_CODE.parse("-1")); assertEquals(Coin.COIN.negate(), NO_CODE.parse("-1.0")); assertEquals(Coin.CENT, NO_CODE.parse(".01")); assertEquals(Coin.MILLICOIN, MonetaryFormat.MBTC.parse("1")); assertEquals(Coin.MILLICOIN, MonetaryFormat.MBTC.parse("1.0")); assertEquals(Coin.MILLICOIN, MonetaryFormat.MBTC.parse("01.0000000000")); assertEquals(Coin.MILLICOIN, MonetaryFormat.MBTC.positiveSign('+').parse("+1.0")); assertEquals(Coin.MILLICOIN.negate(), MonetaryFormat.MBTC.parse("-1")); assertEquals(Coin.MILLICOIN.negate(), MonetaryFormat.MBTC.parse("-1.0")); assertEquals(Coin.MICROCOIN, MonetaryFormat.UBTC.parse("1")); assertEquals(Coin.MICROCOIN, MonetaryFormat.UBTC.parse("1.0")); assertEquals(Coin.MICROCOIN, MonetaryFormat.UBTC.parse("01.0000000000")); assertEquals(Coin.MICROCOIN, MonetaryFormat.UBTC.positiveSign('+').parse("+1.0")); assertEquals(Coin.MICROCOIN.negate(), MonetaryFormat.UBTC.parse("-1")); assertEquals(Coin.MICROCOIN.negate(), MonetaryFormat.UBTC.parse("-1.0")); assertEquals(Coin.CENT, NO_CODE.withLocale(new Locale("hi", "IN")).parse(".०१")); // Devanagari }
public void onSendBitcoin(View view) { String to = mToAddressEdit.getText().toString(); String amount = mAmountEdit.getText().toString(); if (TextUtils.isEmpty(to) || TextUtils.isEmpty(amount)) { return; } Address address = Address.fromBase58(Constants.NETWORK_PARAMETERS, to); Coin coin = MonetaryFormat.MBTC.parse(amount); SendRequest sendRequest = SendRequest.to(address, coin); try { Transaction transaction = wallet.sendCoinsOffline(sendRequest); BlockChainService.broadcastTransaction(BitcoinWalletActivity.this, transaction); } catch (InsufficientMoneyException e) { Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show(); e.printStackTrace(); } }
@Test(expected = NumberFormatException.class) public void parseInvalidNegativeSign() throws Exception { NO_CODE.negativeSign('@').parse("-1.0"); }
@Test(expected = NumberFormatException.class) public void parseInvalidDecimalMark() throws Exception { NO_CODE.decimalMark(',').parse("1.0"); }