Search in sources :

Example 11 with BigDecimal

use of android.icu.math.BigDecimal in project j2objc by google.

the class TimeScaleMonkeyTest method initRandom.

private void initRandom(long min, long max) {
    BigDecimal interval = new BigDecimal(max).subtract(new BigDecimal(min));
    ranMin = min;
    ranMax = max;
    ranInt = 0;
    if (ran == null) {
        ran = createRandom();
    }
    if (interval.compareTo(longMax) < 0) {
        ranInt = interval.longValue();
    }
}
Also used : BigDecimal(android.icu.math.BigDecimal)

Example 12 with BigDecimal

use of android.icu.math.BigDecimal in project j2objc by google.

the class NumberFormatTest method checkRound.

private BigDecimal checkRound(DecimalFormat nf, BigDecimal iValue, BigDecimal lastParsed) {
    String formatedBigDecimal = nf.format(iValue);
    String formattedDouble = nf.format(iValue.doubleValue());
    if (!equalButForTrailingZeros(formatedBigDecimal, formattedDouble)) {
        errln("Failure at: " + iValue + " (" + iValue.doubleValue() + ")" + ",\tRounding-mode: " + roundingModeNames[nf.getRoundingMode()] + ",\tRounding-increment: " + nf.getRoundingIncrement() + ",\tdouble: " + formattedDouble + ",\tBigDecimal: " + formatedBigDecimal);
    } else {
        logln("Value: " + iValue + ",\tRounding-mode: " + roundingModeNames[nf.getRoundingMode()] + ",\tRounding-increment: " + nf.getRoundingIncrement() + ",\tdouble: " + formattedDouble + ",\tBigDecimal: " + formatedBigDecimal);
    }
    try {
        // Number should have compareTo(...)
        BigDecimal parsed = toBigDecimal(nf.parse(formatedBigDecimal));
        if (lastParsed.compareTo(parsed) > 0) {
            errln("Rounding wrong direction!: " + lastParsed + " > " + parsed);
        }
        lastParsed = parsed;
    } catch (ParseException e) {
        errln("Parse Failure with: " + formatedBigDecimal);
    }
    return lastParsed;
}
Also used : ParseException(java.text.ParseException) BigDecimal(android.icu.math.BigDecimal)

Example 13 with BigDecimal

use of android.icu.math.BigDecimal in project j2objc by google.

the class NumberFormatTest method TestBigDecimalRounding.

@Test
public void TestBigDecimalRounding() {
    String figure = "50.000000004";
    Double dbl = new Double(figure);
    BigDecimal dec = new BigDecimal(figure);
    DecimalFormat f = (DecimalFormat) NumberFormat.getInstance();
    f.applyPattern("00.00######");
    assertEquals("double format", "50.00", f.format(dbl));
    assertEquals("bigdec format", "50.00", f.format(dec));
    int maxFracDigits = f.getMaximumFractionDigits();
    BigDecimal roundingIncrement = new BigDecimal("1").movePointLeft(maxFracDigits);
    f.setRoundingIncrement(roundingIncrement);
    f.setRoundingMode(BigDecimal.ROUND_DOWN);
    assertEquals("Rounding down", f.format(dbl), f.format(dec));
    f.setRoundingIncrement(roundingIncrement);
    f.setRoundingMode(BigDecimal.ROUND_HALF_UP);
    assertEquals("Rounding half up", f.format(dbl), f.format(dec));
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) BigDecimal(android.icu.math.BigDecimal) Test(org.junit.Test)

Example 14 with BigDecimal

use of android.icu.math.BigDecimal in project j2objc by google.

the class NumberFormatTest method TestRoundUnnecessarytIssue11808.

// Testing for Issue 11808.
@Test
public void TestRoundUnnecessarytIssue11808() {
    DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance();
    StringBuffer result = new StringBuffer("");
    df.setRoundingMode(BigDecimal.ROUND_UNNECESSARY);
    df.applyPattern("00.0#E0");
    try {
        df.format(99999.0, result, new FieldPosition(0));
        fail("Missing ArithmeticException for double: " + result);
    } catch (ArithmeticException expected) {
    // The exception should be thrown, since rounding is needed.
    }
    try {
        result = df.format(99999, result, new FieldPosition(0));
        fail("Missing ArithmeticException for int: " + result);
    } catch (ArithmeticException expected) {
    // The exception should be thrown, since rounding is needed.
    }
    try {
        result = df.format(new BigInteger("999999"), result, new FieldPosition(0));
        fail("Missing ArithmeticException for BigInteger: " + result);
    } catch (ArithmeticException expected) {
    // The exception should be thrown, since rounding is needed.
    }
    try {
        result = df.format(new BigDecimal("99999"), result, new FieldPosition(0));
        fail("Missing ArithmeticException for BigDecimal: " + result);
    } catch (ArithmeticException expected) {
    // The exception should be thrown, since rounding is needed.
    }
    try {
        result = df.format(new BigDecimal("-99999"), result, new FieldPosition(0));
        fail("Missing ArithmeticException for BigDecimal: " + result);
    } catch (ArithmeticException expected) {
    // The exception should be thrown, since rounding is needed.
    }
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) BigInteger(java.math.BigInteger) FieldPosition(java.text.FieldPosition) BigDecimal(android.icu.math.BigDecimal) Test(org.junit.Test)

Example 15 with BigDecimal

use of android.icu.math.BigDecimal in project j2objc by google.

the class NumberFormatTest method TestCurrency.

/**
 * Test localized currency patterns.
 */
@Test
public void TestCurrency() {
    String[] DATA = { "fr", "CA", "", "1,50\u00a0$", "de", "DE", "", "1,50\u00a0\u20AC", "de", "DE", "PREEURO", "1,50\u00a0DM", "fr", "FR", "", "1,50\u00a0\u20AC", "fr", "FR", "PREEURO", "1,50\u00a0F" };
    for (int i = 0; i < DATA.length; i += 4) {
        Locale locale = new Locale(DATA[i], DATA[i + 1], DATA[i + 2]);
        NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
        String s = fmt.format(1.50);
        if (s.equals(DATA[i + 3])) {
            logln("Ok: 1.50 x " + locale + " => " + s);
        } else {
            logln("FAIL: 1.50 x " + locale + " => " + s + ", expected " + DATA[i + 3]);
        }
    }
    // format currency with CurrencyAmount
    for (int i = 0; i < DATA.length; i += 4) {
        Locale locale = new Locale(DATA[i], DATA[i + 1], DATA[i + 2]);
        Currency curr = Currency.getInstance(locale);
        logln("\nName of the currency is: " + curr.getName(locale, Currency.LONG_NAME, new boolean[] { false }));
        CurrencyAmount cAmt = new CurrencyAmount(1.5, curr);
        // cover hashCode
        logln("CurrencyAmount object's hashCode is: " + cAmt.hashCode());
        NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
        String sCurr = fmt.format(cAmt);
        if (sCurr.equals(DATA[i + 3])) {
            logln("Ok: 1.50 x " + locale + " => " + sCurr);
        } else {
            errln("FAIL: 1.50 x " + locale + " => " + sCurr + ", expected " + DATA[i + 3]);
        }
    }
    // Cover MeasureFormat.getCurrencyFormat()
    ULocale save = ULocale.getDefault();
    ULocale.setDefault(ULocale.US);
    MeasureFormat curFmt = MeasureFormat.getCurrencyFormat();
    String strBuf = curFmt.format(new CurrencyAmount(new Float(1234.56), Currency.getInstance("USD")));
    try {
        CurrencyAmount parsedVal = (CurrencyAmount) curFmt.parseObject(strBuf);
        Number val = parsedVal.getNumber();
        if (!val.equals(new BigDecimal("1234.56"))) {
            errln("FAIL: getCurrencyFormat of default locale (en_US) failed roundtripping the number. val=" + val);
        }
        if (!parsedVal.getCurrency().equals(Currency.getInstance("USD"))) {
            errln("FAIL: getCurrencyFormat of default locale (en_US) failed roundtripping the currency");
        }
    } catch (ParseException e) {
        errln("FAIL: " + e.getMessage());
    }
    ULocale.setDefault(save);
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) ULocale(android.icu.util.ULocale) CurrencyAmount(android.icu.util.CurrencyAmount) BigDecimal(android.icu.math.BigDecimal) Currency(android.icu.util.Currency) ParseException(java.text.ParseException) MeasureFormat(android.icu.text.MeasureFormat) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Aggregations

BigDecimal (android.icu.math.BigDecimal)33 Test (org.junit.Test)19 DecimalFormat (android.icu.text.DecimalFormat)10 CompactDecimalFormat (android.icu.text.CompactDecimalFormat)9 BigInteger (java.math.BigInteger)5 DecimalFormatSymbols (android.icu.text.DecimalFormatSymbols)4 ULocale (android.icu.util.ULocale)4 ParseException (java.text.ParseException)4 NumberFormat (android.icu.text.NumberFormat)3 RuleBasedNumberFormat (android.icu.text.RuleBasedNumberFormat)3 CurrencyAmount (android.icu.util.CurrencyAmount)3 MeasureFormat (android.icu.text.MeasureFormat)2 FieldPosition (java.text.FieldPosition)2 FieldContainer (android.icu.dev.test.format.IntlTestDecimalFormatAPIC.FieldContainer)1 Currency (android.icu.util.Currency)1 AttributedCharacterIterator (java.text.AttributedCharacterIterator)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1