Search in sources :

Example 46 with DecimalFormat

use of android.icu.text.DecimalFormat in project j2objc by google.

the class NumberFormatTest method TestMissingFieldPositionsPerCentPattern.

@Test
public void TestMissingFieldPositionsPerCentPattern() {
    // Check PERCENT with more digits
    DecimalFormatSymbols us_symbols = new DecimalFormatSymbols(ULocale.US);
    DecimalFormat fmtPercent = new DecimalFormat("0.#####%", us_symbols);
    Number number = new Double(-0.986);
    String numFmtted = fmtPercent.format(number);
    checkFormatWithField("sign", fmtPercent, number, numFmtted, NumberFormat.Field.SIGN, 0, 1);
    checkFormatWithField("integer", fmtPercent, number, numFmtted, NumberFormat.Field.INTEGER, 1, 3);
    checkFormatWithField("decimal separator", fmtPercent, number, numFmtted, NumberFormat.Field.DECIMAL_SEPARATOR, 3, 4);
    checkFormatWithField("fraction", fmtPercent, number, numFmtted, NumberFormat.Field.FRACTION, 4, 5);
    checkFormatWithField("percent", fmtPercent, number, numFmtted, NumberFormat.Field.PERCENT, 5, 6);
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 47 with DecimalFormat

use of android.icu.text.DecimalFormat in project j2objc by google.

the class NumberFormatTest method TestParseRequiredDecimalPoint.

@Test
public void TestParseRequiredDecimalPoint() {
    String[] testPattern = { "00.####", "00.0", "00" };
    String value2Parse = "99";
    double parseValue = 99;
    DecimalFormat parser = new DecimalFormat();
    double result;
    boolean hasDecimalPoint;
    for (int i = 0; i < testPattern.length; i++) {
        parser.applyPattern(testPattern[i]);
        hasDecimalPoint = testPattern[i].contains(".");
        parser.setDecimalPatternMatchRequired(false);
        try {
            result = parser.parse(value2Parse).doubleValue();
            assertEquals("wrong parsed value", parseValue, result);
        } catch (ParseException e) {
            TestFmwk.errln("Parsing " + value2Parse + " should have succeeded with " + testPattern[i] + " and isDecimalPointMatchRequired set to: " + parser.isDecimalPatternMatchRequired());
        }
        parser.setDecimalPatternMatchRequired(true);
        try {
            result = parser.parse(value2Parse).doubleValue();
            if (hasDecimalPoint) {
                TestFmwk.errln("Parsing " + value2Parse + " should NOT have succeeded with " + testPattern[i] + " and isDecimalPointMatchRequired set to: " + parser.isDecimalPatternMatchRequired());
            }
        } catch (ParseException e) {
        // OK, should fail
        }
    }
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 48 with DecimalFormat

use of android.icu.text.DecimalFormat in project j2objc by google.

the class NumberFormatTest method TestCustomCurrencySignAndSeparator.

@Test
public void TestCustomCurrencySignAndSeparator() {
    DecimalFormatSymbols custom = new DecimalFormatSymbols(ULocale.US);
    custom.setCurrencySymbol("*");
    custom.setMonetaryGroupingSeparator('^');
    custom.setMonetaryDecimalSeparator(':');
    DecimalFormat fmt = new DecimalFormat("\u00A4 #,##0.00", custom);
    final String numstr = "* 1^234:56";
    expect2(fmt, 1234.56, numstr);
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 49 with DecimalFormat

use of android.icu.text.DecimalFormat in project j2objc by google.

the class NumberFormatTest method TestScientificGrouping.

@Test
public void TestScientificGrouping() {
    // jb 2552
    DecimalFormat fmt = new DecimalFormat("###.##E0");
    expect(fmt, .01234, "12.3E-3");
    expect(fmt, .1234, "123E-3");
    expect(fmt, 1.234, "1.23E0");
    expect(fmt, 12.34, "12.3E0");
    expect(fmt, 123.4, "123E0");
    expect(fmt, 1234, "1.23E3");
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 50 with DecimalFormat

use of android.icu.text.DecimalFormat in project j2objc by google.

the class NumberFormatTest method TestPatterns2.

/**
 * Upgrade to alphaWorks
 */
@Test
public void TestPatterns2() {
    DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
    DecimalFormat fmt = new DecimalFormat("#", US);
    char hat = 0x005E;
    /*^*/
    expectPad(fmt, "*^#", DecimalFormat.PAD_BEFORE_PREFIX, 1, hat);
    expectPad(fmt, "$*^#", DecimalFormat.PAD_AFTER_PREFIX, 2, hat);
    expectPad(fmt, "#*^", DecimalFormat.PAD_BEFORE_SUFFIX, 1, hat);
    expectPad(fmt, "#$*^", DecimalFormat.PAD_AFTER_SUFFIX, 2, hat);
    expectPad(fmt, "$*^$#", -1);
    expectPad(fmt, "#$*^$", -1);
    expectPad(fmt, "'pre'#,##0*x'post'", DecimalFormat.PAD_BEFORE_SUFFIX, 12, (char) 0x0078);
    expectPad(fmt, "''#0*x", DecimalFormat.PAD_BEFORE_SUFFIX, 3, (char) 0x0078);
    expectPad(fmt, "'I''ll'*a###.##", DecimalFormat.PAD_AFTER_PREFIX, 10, (char) 0x0061);
    fmt.applyPattern("AA#,##0.00ZZ");
    fmt.setPadCharacter(hat);
    fmt.setFormatWidth(10);
    fmt.setPadPosition(DecimalFormat.PAD_BEFORE_PREFIX);
    expectPat(fmt, "*^AA#,##0.00ZZ");
    fmt.setPadPosition(DecimalFormat.PAD_BEFORE_SUFFIX);
    expectPat(fmt, "AA#,##0.00*^ZZ");
    fmt.setPadPosition(DecimalFormat.PAD_AFTER_SUFFIX);
    expectPat(fmt, "AA#,##0.00ZZ*^");
    // 12  3456789012
    String exp = "AA*^#,##0.00ZZ";
    fmt.setFormatWidth(12);
    fmt.setPadPosition(DecimalFormat.PAD_AFTER_PREFIX);
    expectPat(fmt, exp);
    fmt.setFormatWidth(13);
    // 12  34567890123
    expectPat(fmt, "AA*^##,##0.00ZZ");
    fmt.setFormatWidth(14);
    // 12  345678901234
    expectPat(fmt, "AA*^###,##0.00ZZ");
    fmt.setFormatWidth(15);
    // 12  3456789012345
    // This is the interesting case
    expectPat(fmt, "AA*^####,##0.00ZZ");
    fmt.setFormatWidth(16);
    // 12  34567890123456
    expectPat(fmt, "AA*^#,###,##0.00ZZ");
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Aggregations

DecimalFormat (android.icu.text.DecimalFormat)150 Test (org.junit.Test)138 CompactDecimalFormat (android.icu.text.CompactDecimalFormat)70 DecimalFormatSymbols (android.icu.text.DecimalFormatSymbols)57 NumberFormat (android.icu.text.NumberFormat)30 ULocale (android.icu.util.ULocale)28 FieldPosition (java.text.FieldPosition)25 ParseException (java.text.ParseException)23 Locale (java.util.Locale)18 ParsePosition (java.text.ParsePosition)15 RuleBasedNumberFormat (android.icu.text.RuleBasedNumberFormat)12 BigDecimal (android.icu.math.BigDecimal)10 IOException (java.io.IOException)8 InvalidObjectException (java.io.InvalidObjectException)5 BigInteger (java.math.BigInteger)5 ScientificNumberFormatter (android.icu.text.ScientificNumberFormatter)4 SimpleDateFormat (android.icu.text.SimpleDateFormat)4 Format (java.text.Format)4 DateFormat (android.icu.text.DateFormat)3 MessageFormat (android.icu.text.MessageFormat)3