/** * Returns a copy of this {@code CurrencyAmount} with the amount negated. * <p> * This takes this amount and negates it. If the amount is 0.0 or -0.0 the negated amount is 0.0. * <p> * This instance is immutable and unaffected by this method. * * @return an amount based on this with the amount negated */ public CurrencyAmount negated() { // Zero is treated as a special case to avoid creating -0.0 which produces surprising equality behaviour return new CurrencyAmount(currency, amount == 0d ? 0d : -amount); }
/** * Obtains an instance of {@code CurrencyAmount} for the specified currency and amount. * * @param currency the currency the amount is in * @param amount the amount of the currency to represent * @return the currency amount */ public static CurrencyAmount of(Currency currency, double amount) { return new CurrencyAmount(currency, amount); }
/** * Returns a copy of this {@code CurrencyAmount} with the specified amount added. * <p> * This adds the specified amount to this monetary amount, returning a new object. * The addition simply uses standard {@code double} arithmetic. * <p> * This instance is immutable and unaffected by this method. * * @param amountToAdd the amount to add * @return an amount based on this with the specified amount added */ public CurrencyAmount plus(double amountToAdd) { return new CurrencyAmount(currency, amount + amountToAdd); }
/** * Returns a copy of this {@code CurrencyAmount} with the specified amount subtracted. * <p> * This subtracts the specified amount to this monetary amount, returning a new object. * The addition simply uses standard {@code double} arithmetic. * <p> * This instance is immutable and unaffected by this method. * * @param amountToSubtract the amount to subtract * @return an amount based on this with the specified amount subtracted */ public CurrencyAmount minus(double amountToSubtract) { return new CurrencyAmount(currency, amount - amountToSubtract); }
/** * Returns a copy of this {@code CurrencyAmount} with the amount multiplied. * <p> * This takes this amount and multiplies it by the specified value. * The multiplication simply uses standard {@code double} arithmetic. * <p> * This instance is immutable and unaffected by this method. * * @param valueToMultiplyBy the scalar amount to multiply by * @return an amount based on this with the amount multiplied */ public CurrencyAmount multipliedBy(double valueToMultiplyBy) { return new CurrencyAmount(currency, amount * valueToMultiplyBy); }
CompactDecimalFormat cdFormat = CompactDecimalFormat.getInstance( Locale.US, CompactStyle.SHORT,NumberFormat.CURRENCYSTYLE); Currency currency = Currency.getInstance(Locale.US); CurrencyAmount amount = new CurrencyAmount(12345.67, currency); System.out.println(cdFormat.format(amount));
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.US); Currency currency = Currency.getInstance(Locale.US); CurrencyAmount amount = new CurrencyAmount(12345.67, currency); System.out.println(numberFormat.format(amount));
/** * Parses the string to produce a {@code CurrencyAmount}. * <p> * This parses the {@code toString} format of '${currency} ${amount}'. * * @param amountStr the amount string * @return the currency amount * @throws IllegalArgumentException if the amount cannot be parsed */ @FromString public static CurrencyAmount parse(String amountStr) { ArgChecker.notNull(amountStr, "amountStr"); List<String> split = Splitter.on(' ').splitToList(amountStr); if (split.size() != 2) { throw new IllegalArgumentException("Unable to parse amount, invalid format: " + amountStr); } try { Currency cur = Currency.parse(split.get(0)); double amount = Double.parseDouble(split.get(1)); return new CurrencyAmount(cur, amount); } catch (RuntimeException ex) { throw new IllegalArgumentException("Unable to parse amount: " + amountStr, ex); } }
/** * Applies an operation to the amount. * <p> * This is generally used to apply a mathematical operation to the amount. * For example, the operator could multiply the amount by a constant, or take the inverse. * <pre> * multiplied = base.mapAmount(value -> (value < 0 ? 0 : value * 3)); * </pre> * * @param mapper the operator to be applied to the amount * @return a copy of this amount with the mapping applied to the original amount */ public CurrencyAmount mapAmount(DoubleUnaryOperator mapper) { ArgChecker.notNull(mapper, "mapper"); return new CurrencyAmount(currency, mapper.applyAsDouble(amount)); }