/** * Obtains an instance that rounds to the number of minor units in the currency. * <p> * This returns a convention that rounds for the specified currency. * Rounding follows the normal {@link RoundingMode#HALF_UP} convention. * * @param currency the currency * @return the rounding convention */ public static Rounding of(Currency currency) { return HalfUpRounding.ofDecimalPlaces(currency.getMinorUnitDigits()); }
/** * Gets the number of digits in the rate. * <p> * If this rate is a conventional currency pair defined in configuration, * then the number of digits in a market FX rate quote is returned. * <p> * If the currency pair is not defined in configuration the sum of the * {@link Currency#getMinorUnitDigits() minor unit digits} from the two currencies is returned. * * @return the number of digits in the FX rate */ public int getRateDigits() { Integer digits = CONFIGURED.get(this); if (digits != null) { return digits; } Integer inverseDigits = CONFIGURED.get(inverse()); if (inverseDigits != null) { return inverseDigits; } return base.getMinorUnitDigits() + counter.getMinorUnitDigits(); }
/** * Obtains an instance from the currency. * <p> * This sets the tick size and tick value to the minor unit of the currency. * For example, for USD this will set the tick size to 0.01 and the tick value to $0.01. * This typically matches the conventions of equities and bonds. * * @param currency the currency to derive the price information from * @return the security price information */ public static SecurityPriceInfo ofCurrencyMinorUnit(Currency currency) { int digits = currency.getMinorUnitDigits(); double unitAmount = Math.pow(10, -digits); return new SecurityPriceInfo(unitAmount, CurrencyAmount.of(currency, unitAmount), 1); }
public void test_minorUnits() { assertEquals(Currency.of("USD").getMinorUnitDigits(), 2); assertEquals(Currency.of("EUR").getMinorUnitDigits(), 2); assertEquals(Currency.of("JPY").getMinorUnitDigits(), 0); assertEquals(Currency.of("GBP").getMinorUnitDigits(), 2); assertEquals(Currency.of("CHF").getMinorUnitDigits(), 2); assertEquals(Currency.of("AUD").getMinorUnitDigits(), 2); assertEquals(Currency.of("CAD").getMinorUnitDigits(), 2); }
@Override public String formatForDisplay(CurrencyAmount amount) { return doubleFormatter.formatForDisplay(amount.getAmount(), amount.getCurrency().getMinorUnitDigits()); }
/** * Converts an FX exchange to a string. * * @param base the base currency amount * @param counter the counter currency amount * @return the string form */ public static String fx(CurrencyAmount base, CurrencyAmount counter) { BigDecimal rateDec = BigDecimal.valueOf( counter.getAmount() / base.getAmount()).setScale(base.getCurrency().getMinorUnitDigits() + 2, RoundingMode.HALF_UP).abs(); FxRate rate = FxRate.of(base.getCurrency(), counter.getCurrency(), rateDec.doubleValue()); BigDecimal baseDec = BigDecimal.valueOf(base.getAmount()).stripTrailingZeros(); BigDecimal counterDec = BigDecimal.valueOf(counter.getAmount()).stripTrailingZeros(); boolean roundBase = baseDec.scale() < counterDec.scale(); CurrencyAmount round = roundBase ? base : counter; return (round.getAmount() < 0 ? "Pay " : "Rec ") + SummarizerUtils.amount(round.mapAmount(a -> Math.abs(a))) + " " + "@ " + rate; }
public void test_of_String_unknownCurrencyCreated() { Currency test = Currency.of("AAA"); assertEquals(test.getCode(), "AAA"); assertEquals(test.getMinorUnitDigits(), 0); assertSame(test, Currency.of("AAA")); }
public void test_parse_String_unknownCurrencyCreated() { Currency test = Currency.parse("zyx"); assertEquals(test.getCode(), "ZYX"); assertEquals(test.getMinorUnitDigits(), 0); assertSame(test, Currency.of("ZYX")); }
public void test_of_String_historicCurrency() { Currency test = Currency.of("BEF"); assertEquals(test.getCode(), "BEF"); assertEquals(test.getMinorUnitDigits(), 2); assertEquals(test.getTriangulationCurrency(), Currency.EUR); assertSame(test, Currency.of("BEF")); }