org.bitcoinj.utils.MonetaryFormat Java Examples

The following examples show how to use org.bitcoinj.utils.MonetaryFormat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BSFormatter.java    From bisq-core with GNU Affero General Public License v3.0 6 votes vote down vote up
public String formatFiat(Fiat fiat, MonetaryFormat format, boolean appendCurrencyCode) {
    if (fiat != null) {
        try {
            final String res = format.noCode().format(fiat).toString();
            if (appendCurrencyCode)
                return res + " " + fiat.getCurrencyCode();
            else
                return res;
        } catch (Throwable t) {
            log.warn("Exception at formatFiatWithCode: " + t.toString());
            return Res.get("shared.na") + " " + fiat.getCurrencyCode();
        }
    } else {
        return Res.get("shared.na");
    }
}
 
Example #2
Source File: FormattingUtils.java    From bisq with GNU Affero General Public License v3.0 6 votes vote down vote up
public static String formatFiat(Fiat fiat, MonetaryFormat format, boolean appendCurrencyCode) {
    if (fiat != null) {
        try {
            final String res = format.noCode().format(fiat).toString();
            if (appendCurrencyCode)
                return res + " " + fiat.getCurrencyCode();
            else
                return res;
        } catch (Throwable t) {
            log.warn("Exception at formatFiatWithCode: " + t.toString());
            return Res.get("shared.na") + " " + fiat.getCurrencyCode();
        }
    } else {
        return Res.get("shared.na");
    }
}
 
Example #3
Source File: BsqFormatter.java    From bisq-core with GNU Affero General Public License v3.0 6 votes vote down vote up
@Inject
private BsqFormatter() {
    super();

    final String baseCurrencyCode = BisqEnvironment.getBaseCurrencyNetwork().getCurrencyCode();
    switch (baseCurrencyCode) {
        case "BTC":
            coinFormat = new MonetaryFormat().shift(6).code(6, "BSQ").minDecimals(2);
            break;
        case "LTC":
            coinFormat = new MonetaryFormat().shift(3).code(3, "BSQ").minDecimals(5);
            break;
        case "DASH":
            // BSQ for DASH not used/supported
            coinFormat = new MonetaryFormat().shift(3).code(3, "???").minDecimals(5);
            break;
        default:
            throw new RuntimeException("baseCurrencyCode not defined. baseCurrencyCode=" + baseCurrencyCode);
    }

    amountFormat.setMinimumFractionDigits(2);
}
 
Example #4
Source File: FormattingUtils.java    From bisq with GNU Affero General Public License v3.0 6 votes vote down vote up
public static String formatCoin(Coin coin,
                                int decimalPlaces,
                                boolean decimalAligned,
                                int maxNumberOfDigits,
                                MonetaryFormat coinFormat) {
    String formattedCoin = "";

    if (coin != null) {
        try {
            if (decimalPlaces < 0 || decimalPlaces > 4) {
                formattedCoin = coinFormat.noCode().format(coin).toString();
            } else {
                formattedCoin = coinFormat.noCode().minDecimals(decimalPlaces).repeatOptionalDecimals(1, decimalPlaces).format(coin).toString();
            }
        } catch (Throwable t) {
            log.warn("Exception at formatBtc: " + t.toString());
        }
    }

    if (decimalAligned) {
        formattedCoin = fillUpPlacesWithEmptyStrings(formattedCoin, maxNumberOfDigits);
    }

    return formattedCoin;
}
 
Example #5
Source File: MainController.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
public void onBitcoinSetup() {
    model.setWallet(bitcoin.wallet());
    addressControl.addressProperty().bind(model.addressProperty());
    balance.textProperty().bind(EasyBind.map(model.balanceProperty(), coin -> MonetaryFormat.BTC.noCode().format(coin).toString()));
    // Don't let the user click send money when the wallet is empty.
    sendMoneyOutBtn.disableProperty().bind(model.balanceProperty().isEqualTo(Coin.ZERO));

    showBitcoinSyncMessage();
    model.syncProgressProperty().addListener(x -> {
        if (model.syncProgressProperty().get() >= 1.0) {
            readyToGoAnimation();
            if (syncItem != null) {
                syncItem.cancel();
                syncItem = null;
            }
        } else if (syncItem == null) {
            showBitcoinSyncMessage();
        }
    });
}
 
Example #6
Source File: MainController.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
public void onBitcoinSetup() {
    model.setWallet(bitcoin.wallet());
    addressControl.addressProperty().bind(model.addressProperty());
    balance.textProperty().bind(EasyBind.map(model.balanceProperty(), coin -> MonetaryFormat.BTC.noCode().format(coin).toString()));
    // Don't let the user click send money when the wallet is empty.
    sendMoneyOutBtn.disableProperty().bind(model.balanceProperty().isEqualTo(Coin.ZERO));

    showBitcoinSyncMessage();
    model.syncProgressProperty().addListener(x -> {
        if (model.syncProgressProperty().get() >= 1.0) {
            readyToGoAnimation();
            if (syncItem != null) {
                syncItem.cancel();
                syncItem = null;
            }
        } else if (syncItem == null) {
            showBitcoinSyncMessage();
        }
    });
}
 
Example #7
Source File: WalletTransaction.java    From cate with MIT License 5 votes vote down vote up
protected WalletTransaction(final Network network, final Transaction transaction, final Coin balanceChange) {
    this.network = network;
    this.transaction = transaction;
    this.balanceChange = balanceChange;
    final MonetaryFormat monetaryFormatter = network.getParams().getMonetaryFormat();
    dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
    networkNameProperty = new SimpleStringProperty(NetworkResolver.getName(network.getParams()));
    dateProperty = new SimpleStringProperty(dateFormat.format(transaction.getUpdateTime()));
    amountProperty = new SimpleStringProperty(monetaryFormatter.format(balanceChange).toString());
    memoProperty = new SimpleStringProperty(transaction.getMemo());
    memoProperty.addListener(change -> {
        transaction.setMemo(memoProperty.getValue());
    });
}
 
Example #8
Source File: ParsingUtils.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Coin parseToCoin(String input, MonetaryFormat coinFormat) {
    if (input != null && input.length() > 0) {
        try {
            return coinFormat.parse(cleanDoubleInput(input));
        } catch (Throwable t) {
            log.warn("Exception at parseToBtc: " + t.toString());
            return Coin.ZERO;
        }
    } else {
        return Coin.ZERO;
    }
}
 
Example #9
Source File: BsqFormatter.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
@Inject
public BsqFormatter() {
    this.btcCoinFormat = Config.baseCurrencyNetworkParameters().getMonetaryFormat();
    this.monetaryFormat = new MonetaryFormat().shift(6).code(6, "BSQ").minDecimals(2);
    this.immutableCoinFormatter = new ImmutableCoinFormatter(monetaryFormat);

    GlobalSettings.localeProperty().addListener((observable, oldValue, newValue) -> switchLocale(newValue));
    switchLocale(GlobalSettings.getLocale());

    amountFormat.setMinimumFractionDigits(2);
}
 
Example #10
Source File: FormattingUtils.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
public static 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");
    }
}
 
Example #11
Source File: FormattingUtils.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
public static String formatCoinWithCode(Coin coin, MonetaryFormat coinFormat) {
    if (coin != null) {
        try {
            // we don't use the code feature from coinFormat as it does automatic switching between mBTC and BTC and
            // pre and post fixing
            return coinFormat.postfixCode().format(coin).toString();
        } catch (Throwable t) {
            log.warn("Exception at formatCoinWithCode: " + t.toString());
            return "";
        }
    } else {
        return "";
    }
}
 
Example #12
Source File: DisplayUtils.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
private static String formatVolume(Volume volume, MonetaryFormat fiatVolumeFormat, boolean appendCurrencyCode) {
    if (volume != null) {
        Monetary monetary = volume.getMonetary();
        if (monetary instanceof Fiat)
            return FormattingUtils.formatFiat((Fiat) monetary, fiatVolumeFormat, appendCurrencyCode);
        else
            return FormattingUtils.formatAltcoinVolume((Altcoin) monetary, appendCurrencyCode);
    } else {
        return "";
    }
}
 
Example #13
Source File: UI.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private static MonetaryFormat getUnitFormat(final GaService service) {
    final String unit = service.getBitcoinUnit();
    if (MonetaryFormat.CODE_BTC.equals(unit))
        return BTC;
    if (MonetaryFormat.CODE_MBTC.equals(unit))
        return MBTC;
    return UBTC;
}
 
Example #14
Source File: GaService.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public String coinToFiat(final Coin btcValue) {
    if (!hasFiatRate())
        return "N/A";
    Fiat fiatValue = getFiatRate().coinToFiat(btcValue);
    // strip extra decimals (over 2 places) because that's what the old JS client does
    fiatValue = fiatValue.subtract(fiatValue.divideAndRemainder((long) Math.pow(10, Fiat.SMALLEST_UNIT_EXPONENT - 2))[1]);
    return MonetaryFormat.FIAT.minDecimals(2).noCode().format(fiatValue).toString();
}
 
Example #15
Source File: BSFormatter.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
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 "";
    }
}
 
Example #16
Source File: BSFormatter.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
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");
    }
}
 
Example #17
Source File: UI.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private static int getUnitSymbol(final GaService service) {
    final String unit = service.getBitcoinUnit();
    if (MonetaryFormat.CODE_BTC.equals(unit))
        return R.string.fa_btc_space;
    if (MonetaryFormat.CODE_MBTC.equals(unit))
        return R.string.fa_mbtc_space;
    if (MICRO_BTC.equals(unit))
        return R.string.fa_ubtc_space;
    return R.string.fa_bits_space;
}
 
Example #18
Source File: AbstractBitcoinNetParams.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public MonetaryFormat getMonetaryFormat() {
    return new MonetaryFormat();
}
 
Example #19
Source File: AbstractBitcoinNetParams.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public MonetaryFormat getMonetaryFormat() {
    return new MonetaryFormat();
}
 
Example #20
Source File: ImmutableCoinFormatter.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
@Inject
public ImmutableCoinFormatter(MonetaryFormat monetaryFormat) {
    this.monetaryFormat = monetaryFormat;
}
 
Example #21
Source File: AbstractBitcoinNetParams.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public MonetaryFormat getMonetaryFormat() {
    return MonetaryFormat.BTC;
}
 
Example #22
Source File: NetworkParametersAdapter.java    From bisq-assets with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public MonetaryFormat getMonetaryFormat() {
    return null;
}
 
Example #23
Source File: FormattingUtils.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
public static String formatCoin(long value, MonetaryFormat coinFormat) {
    return formatCoin(Coin.valueOf(value), -1, false, 0, coinFormat);
}
 
Example #24
Source File: FormattingUtils.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
public static String formatCoinWithCode(long value, MonetaryFormat coinFormat) {
    return formatCoinWithCode(Coin.valueOf(value), coinFormat);
}
 
Example #25
Source File: NetworkParametersAdapter.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public MonetaryFormat getMonetaryFormat() {
    return null;
}
 
Example #26
Source File: AbstractBitcoinNetParams.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
public MonetaryFormat getMonetaryFormat() {
    return new MonetaryFormat();
}
 
Example #27
Source File: GaService.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
public MonetaryFormat getAssetFormat() {
    return mAssetFormat;
}
 
Example #28
Source File: NetworkParameters.java    From green_android with GNU General Public License v3.0 2 votes vote down vote up
/**
 * The monetary object for this currency.
 */
public abstract MonetaryFormat getMonetaryFormat();
 
Example #29
Source File: NetworkParameters.java    From GreenBits with GNU General Public License v3.0 2 votes vote down vote up
/**
 * The monetary object for this currency.
 */
public abstract MonetaryFormat getMonetaryFormat();
 
Example #30
Source File: NetworkParameters.java    From bcm-android with GNU General Public License v3.0 2 votes vote down vote up
/**
 * The monetary object for this currency.
 */
public abstract MonetaryFormat getMonetaryFormat();