private boolean isValidAmount(final boolean zeroIsValid) { final String str = textView.getText().toString().trim(); try { if (!str.isEmpty()) { final Monetary amount; if (localCurrencyCode == null) { amount = inputFormat.parse(str); if (((Coin) amount).isGreaterThan(Constants.NETWORK_PARAMETERS.getMaxMoney())) return false; } else { amount = inputFormat.parseFiat(localCurrencyCode, str); } // exactly zero return zeroIsValid || amount.signum() > 0; } } catch (final Exception x) { } return false; }
private boolean isValidAmount(final boolean zeroIsValid) { final String str = textView.getText().toString().trim(); try { if (!str.isEmpty()) { final Monetary amount; if (localCurrencyCode == null) amount = inputFormat.parse(str); else amount = inputFormat.parseFiat(localCurrencyCode, str); // exactly zero return zeroIsValid || amount.signum() > 0; } } catch (final Exception x) { } return false; }
@Nullable public Monetary getAmount() { if (!isValidAmount(false)) return null; final String amountStr = textView.getText().toString().trim(); if (localCurrencyCode == null) return inputFormat.parse(amountStr); else return inputFormat.parseFiat(localCurrencyCode, amountStr); }
public void setAmount(@Nullable final Monetary amount, final boolean fireListener) { if (!fireListener) textViewListener.setFire(false); if (amount != null) textView.setText(new MonetarySpannable(inputFormat, amountSigned, amount)); else textView.setText(null); if (!fireListener) textViewListener.setFire(true); }
@Override protected void onRestoreInstanceState(final Parcelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; super.onRestoreInstanceState(bundle.getParcelable("super_state")); textView.onRestoreInstanceState(bundle.getParcelable("child_textview")); setAmount((Monetary) bundle.getSerializable("amount"), false); } else { super.onRestoreInstanceState(state); } }
@Override public void onFocusChange(final View v, final boolean hasFocus) { if (!hasFocus) { final Monetary amount = getAmount(); if (amount != null) setAmount(amount, false); } if (listener != null && fire) listener.focusChanged(hasFocus); }
@Override public boolean equals(final Object o) { if (o == this) return true; if (o == null || o.getClass() != getClass()) return false; final Monetary otherMonetary = ((MonetaryWrapper) o).getMonetary(); return monetary.getValue() == otherMonetary.getValue(); }
public Coin getAmountByVolume(Volume volume) { Monetary monetary = volume.getMonetary(); if (monetary instanceof Fiat && this.monetary instanceof Fiat) return new ExchangeRate((Fiat) this.monetary).fiatToCoin((Fiat) monetary); else if (monetary instanceof Altcoin && this.monetary instanceof Altcoin) return new AltcoinExchangeRate((Altcoin) this.monetary).altcoinToCoin((Altcoin) monetary); else return Coin.ZERO; }
public String formatVolume(Volume volume, MonetaryFormat fiatVolumeFormat, boolean appendCurrencyCode) { if (volume != null) { Monetary monetary = volume.getMonetary(); if (monetary instanceof Fiat) return formatFiat((Fiat) monetary, fiatVolumeFormat, appendCurrencyCode); else return formatAltcoinVolume((Altcoin) monetary, appendCurrencyCode); } else { return ""; } }
public String formatPrice(Price price, MonetaryFormat fiatPriceFormat, boolean appendCurrencyCode) { if (price != null) { Monetary monetary = price.getMonetary(); if (monetary instanceof Fiat) return formatFiat((Fiat) monetary, fiatPriceFormat, appendCurrencyCode); else return formatAltcoin((Altcoin) monetary, appendCurrencyCode); } else { return Res.get("shared.na"); } }
public String formatPriceWithCode(Price price) { Monetary monetary = price.getMonetary(); if (monetary instanceof Fiat) return formatFiat((Fiat) monetary, fiatPriceFormat, true); else { return formatAltcoinWithCode((Altcoin) monetary); } //return formatPrice(fiat) + " " + getCurrencyPair(fiat.getCurrencyCode()); }
public void setAmount(final Monetary amount) { this.amount = amount; updateView(); }
public void setHint(@Nullable final Monetary hint) { this.hint = hint; updateAppearance(); }
public MonetarySpannable(final MonetaryFormat format, final boolean signed, @Nullable final Monetary monetary) { super(format(format, signed, monetary)); }
public MonetarySpannable(final MonetaryFormat format, @Nullable final Monetary monetary) { super(format(format, false, monetary)); }
public MonetarySpannable(final MonetaryFormat format, final boolean signed, @Nullable final Monetary value) { super(format(format, signed, value)); }
public MonetarySpannable(final MonetaryFormat format, @Nullable final Monetary value) { super(format(format, false, value)); }
/** * Format the given monetary value to a human readable form. */ public CharSequence format(Monetary monetary) { return format(monetary, monetary.smallestUnitExponent()); }
public static String formatCoinValue(@Nonnull final ValueType type, @Nonnull final Monetary value) { return formatCoinValue(type, value, "", "-", 8, 0); }
public static String formatCoinValue(@Nonnull final ValueType type, @Nonnull final Monetary value, final int precision, final int shift) { return formatCoinValue(type, value, "", "-", precision, shift); }
public static String formatCoinValue(@Nonnull final ValueType type, @Nonnull final Monetary value, @Nonnull final String plusSign, @Nonnull final String minusSign, final int precision, final int shift) { return formatValue(type.getUnitExponent(), value, plusSign, minusSign, precision, shift, false); }
public static String formatCoinValue(@Nonnull final ValueType type, @Nonnull final Monetary value, boolean removeFinalZeroes) { return formatValue(type.getUnitExponent(), value, "", "-", 8, 0, removeFinalZeroes); }
private static String formatValue(final long unitExponent, @Nonnull final Monetary value, @Nonnull final String plusSign, @Nonnull final String minusSign, final int precision, final int shift, boolean removeFinalZeroes) { long longValue = value.getValue(); final String sign = value.signum() == -1 ? minusSign : plusSign; String formatedValue; if (shift == 0) { long units = Math.round(Math.pow(10, unitExponent)); long precisionUnits = (long) (units / Math.pow(10, precision)); long roundingPrecisionUnits = precisionUnits / 2; if (precision == 2 || precision == 4 || precision == 6 || precision == 8) { if (roundingPrecisionUnits > 0) { longValue = longValue - longValue % precisionUnits + longValue % precisionUnits / roundingPrecisionUnits * precisionUnits; } } else { throw new IllegalArgumentException("cannot handle precision/shift: " + precision + "/" + shift); } final long absValue = Math.abs(longValue); final long coins = absValue / units; final int satoshis = (int) (absValue % units); if (isShiftPossible(units, satoshis, 100)) { formatedValue = String.format(Locale.US, "%d.%02d", coins, getShiftedCents(units, satoshis, 100)); } else if (isShiftPossible(units, satoshis, 10000)) { formatedValue = String.format(Locale.US, "%d.%04d", coins, getShiftedCents(units, satoshis, 10000)); } else if (isShiftPossible(units, satoshis, 1000000)) { formatedValue = String.format(Locale.US, "%d.%06d", coins, getShiftedCents(units, satoshis, 1000000)); } else { formatedValue = String.format(Locale.US, "%d.%08d", coins, satoshis); } } else { throw new IllegalArgumentException("cannot handle shift: " + shift); } // Relax precision if incorrectly shows value as 0.00 but is in reality not zero if (formatedValue.equals("0.00") && value.getValue() != 0) { return formatValue(unitExponent, value, plusSign, minusSign, precision + 2, shift, removeFinalZeroes); } // Remove final zeroes if requested while (removeFinalZeroes && formatedValue.length() > 0 && formatedValue.contains(".") && formatedValue.endsWith("0")) { formatedValue = formatedValue.substring(0, formatedValue.length() - 1); } if (removeFinalZeroes && formatedValue.length() > 0 && formatedValue.endsWith(".")) { formatedValue = formatedValue.substring(0, formatedValue.length() - 1); } // Add the sign if needed formatedValue = String.format(Locale.US, "%s%s", sign, formatedValue); return formatedValue; }
public MonetaryWrapper(Monetary monetary) { this.monetary = monetary; }
public Monetary getMonetary() { return monetary; }
public Price(Monetary monetary) { super(monetary); }
public Volume(Monetary monetary) { super(monetary); }