Search in sources :

Example 96 with DecimalFormat

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

the class NumberFormatTest method TestQuotes.

// Test the handling of quotes
@Test
public void TestQuotes() {
    StringBuffer pat;
    DecimalFormatSymbols sym = new DecimalFormatSymbols(Locale.US);
    pat = new StringBuffer("a'fo''o'b#");
    DecimalFormat fmt = new DecimalFormat(pat.toString(), sym);
    String s = ((NumberFormat) fmt).format(123);
    logln("Pattern \"" + pat + "\"");
    logln(" Format 123 . " + s);
    if (!s.equals("afo'ob123"))
        errln("FAIL: Expected afo'ob123");
    s = "";
    pat = new StringBuffer("a''b#");
    fmt = new DecimalFormat(pat.toString(), sym);
    s = ((NumberFormat) fmt).format(123);
    logln("Pattern \"" + pat + "\"");
    logln(" Format 123 . " + s);
    if (!s.equals("a'b123"))
        errln("FAIL: Expected a'b123");
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 97 with DecimalFormat

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

the class NumberFormatTest method TestMissingFieldPositionsNegativeLong.

@Test
public void TestMissingFieldPositionsNegativeLong() {
    Number number = new Long("-123456789987654321");
    DecimalFormatSymbols us_symbols = new DecimalFormatSymbols(ULocale.US);
    DecimalFormat formatter = new DecimalFormat("0.#####E+0", us_symbols);
    String longFmtted = formatter.format(number);
    checkFormatWithField("sign", formatter, number, longFmtted, NumberFormat.Field.SIGN, 0, 1);
    checkFormatWithField("integer", formatter, number, longFmtted, NumberFormat.Field.INTEGER, 1, 2);
    checkFormatWithField("decimal separator", formatter, number, longFmtted, NumberFormat.Field.DECIMAL_SEPARATOR, 2, 3);
    checkFormatWithField("exponent symbol", formatter, number, longFmtted, NumberFormat.Field.EXPONENT_SYMBOL, 8, 9);
    checkFormatWithField("exponent sign", formatter, number, longFmtted, NumberFormat.Field.EXPONENT_SIGN, 9, 10);
    checkFormatWithField("exponent", formatter, number, longFmtted, NumberFormat.Field.EXPONENT, 10, 12);
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 98 with DecimalFormat

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

the class NumberFormatTest method TestPad.

/**
 * Upgrade to alphaWorks
 */
@Test
public void TestPad() {
    DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
    expect2(new DecimalFormat("*^##.##", US), 0, "^^^^0");
    expect2(new DecimalFormat("*^##.##", US), -1.3, "^-1.3");
    expect2(new DecimalFormat("##0.0####E0*_ 'g-m/s^2'", US), 0, "0.0E0______ g-m/s^2");
    expect(new DecimalFormat("##0.0####E0*_ 'g-m/s^2'", US), 1.0 / 3, "333.333E-3_ g-m/s^2");
    expect2(new DecimalFormat("##0.0####*_ 'g-m/s^2'", US), 0, "0.0______ g-m/s^2");
    expect(new DecimalFormat("##0.0####*_ 'g-m/s^2'", US), 1.0 / 3, "0.33333__ g-m/s^2");
    // Test padding before a sign
    final String formatStr = "*x#,###,###,##0.0#;*x(###,###,##0.0#)";
    expect2(new DecimalFormat(formatStr, US), -10, "xxxxxxxxxx(10.0)");
    expect2(new DecimalFormat(formatStr, US), -1000, "xxxxxxx(1,000.0)");
    expect2(new DecimalFormat(formatStr, US), -1000000, "xxx(1,000,000.0)");
    expect2(new DecimalFormat(formatStr, US), -100.37, "xxxxxxxx(100.37)");
    expect2(new DecimalFormat(formatStr, US), -10456.37, "xxxxx(10,456.37)");
    expect2(new DecimalFormat(formatStr, US), -1120456.37, "xx(1,120,456.37)");
    expect2(new DecimalFormat(formatStr, US), -112045600.37, "(112,045,600.37)");
    expect2(new DecimalFormat(formatStr, US), -1252045600.37, "(1,252,045,600.37)");
    expect2(new DecimalFormat(formatStr, US), 10, "xxxxxxxxxxxx10.0");
    expect2(new DecimalFormat(formatStr, US), 1000, "xxxxxxxxx1,000.0");
    expect2(new DecimalFormat(formatStr, US), 1000000, "xxxxx1,000,000.0");
    expect2(new DecimalFormat(formatStr, US), 100.37, "xxxxxxxxxx100.37");
    expect2(new DecimalFormat(formatStr, US), 10456.37, "xxxxxxx10,456.37");
    expect2(new DecimalFormat(formatStr, US), 1120456.37, "xxxx1,120,456.37");
    expect2(new DecimalFormat(formatStr, US), 112045600.37, "xx112,045,600.37");
    expect2(new DecimalFormat(formatStr, US), 10252045600.37, "10,252,045,600.37");
    // Test padding between a sign and a number
    final String formatStr2 = "#,###,###,##0.0#*x;(###,###,##0.0#*x)";
    expect2(new DecimalFormat(formatStr2, US), -10, "(10.0xxxxxxxxxx)");
    expect2(new DecimalFormat(formatStr2, US), -1000, "(1,000.0xxxxxxx)");
    expect2(new DecimalFormat(formatStr2, US), -1000000, "(1,000,000.0xxx)");
    expect2(new DecimalFormat(formatStr2, US), -100.37, "(100.37xxxxxxxx)");
    expect2(new DecimalFormat(formatStr2, US), -10456.37, "(10,456.37xxxxx)");
    expect2(new DecimalFormat(formatStr2, US), -1120456.37, "(1,120,456.37xx)");
    expect2(new DecimalFormat(formatStr2, US), -112045600.37, "(112,045,600.37)");
    expect2(new DecimalFormat(formatStr2, US), -1252045600.37, "(1,252,045,600.37)");
    expect2(new DecimalFormat(formatStr2, US), 10, "10.0xxxxxxxxxxxx");
    expect2(new DecimalFormat(formatStr2, US), 1000, "1,000.0xxxxxxxxx");
    expect2(new DecimalFormat(formatStr2, US), 1000000, "1,000,000.0xxxxx");
    expect2(new DecimalFormat(formatStr2, US), 100.37, "100.37xxxxxxxxxx");
    expect2(new DecimalFormat(formatStr2, US), 10456.37, "10,456.37xxxxxxx");
    expect2(new DecimalFormat(formatStr2, US), 1120456.37, "1,120,456.37xxxx");
    expect2(new DecimalFormat(formatStr2, US), 112045600.37, "112,045,600.37xx");
    expect2(new DecimalFormat(formatStr2, US), 10252045600.37, "10,252,045,600.37");
    // testing the setPadCharacter(UnicodeString) and getPadCharacterString()
    DecimalFormat fmt = new DecimalFormat("#", US);
    char padString = 'P';
    fmt.setPadCharacter(padString);
    expectPad(fmt, "*P##.##", DecimalFormat.PAD_BEFORE_PREFIX, 5, padString);
    fmt.setPadCharacter('^');
    expectPad(fmt, "*^#", DecimalFormat.PAD_BEFORE_PREFIX, 1, '^');
// commented untill implementation is complete
/*  fmt.setPadCharacter((UnicodeString)"^^^");
          expectPad(fmt, "*^^^#", DecimalFormat.kPadBeforePrefix, 3, (UnicodeString)"^^^");
          padString.remove();
          padString.append((UChar)0x0061);
          padString.append((UChar)0x0302);
          fmt.setPadCharacter(padString);
          UChar patternChars[]={0x002a, 0x0061, 0x0302, 0x0061, 0x0302, 0x0023, 0x0000};
          UnicodeString pattern(patternChars);
          expectPad(fmt, pattern , DecimalFormat.kPadBeforePrefix, 4, padString);
         */
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 99 with DecimalFormat

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

the class NumberFormatTest method TestFieldPositionInteger.

@Test
public void TestFieldPositionInteger() {
    DecimalFormat nf = (DecimalFormat) android.icu.text.NumberFormat.getInstance(ULocale.ENGLISH);
    nf.setPositivePrefix("FOO");
    nf.setPositiveSuffix("BA");
    StringBuffer buffer = new StringBuffer();
    FieldPosition fp = new FieldPosition(NumberFormat.Field.INTEGER);
    nf.format(35.47, buffer, fp);
    assertEquals("35.47", "FOO35.47BA", buffer.toString());
    assertEquals("fp begin", 3, fp.getBeginIndex());
    assertEquals("fp end", 5, fp.getEndIndex());
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) FieldPosition(java.text.FieldPosition) Test(org.junit.Test)

Example 100 with DecimalFormat

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

the class NumberFormatTest method TestBug9936.

@Test
public void TestBug9936() {
    DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getInstance(ULocale.US);
    assertFalse("", numberFormat.areSignificantDigitsUsed());
    numberFormat.setSignificantDigitsUsed(true);
    assertTrue("", numberFormat.areSignificantDigitsUsed());
    numberFormat.setSignificantDigitsUsed(false);
    assertFalse("", numberFormat.areSignificantDigitsUsed());
    numberFormat.setMinimumSignificantDigits(3);
    assertTrue("", numberFormat.areSignificantDigitsUsed());
    numberFormat.setSignificantDigitsUsed(false);
    numberFormat.setMaximumSignificantDigits(6);
    assertTrue("", numberFormat.areSignificantDigitsUsed());
}
Also used : 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