android.icu.text.DecimalFormatSymbols Java Examples

The following examples show how to use android.icu.text.DecimalFormatSymbols. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CurrencyTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestDeprecatedCurrencyFormat() {
    // bug 5952
    Locale locale = new Locale("sr", "QQ");
    DecimalFormatSymbols icuSymbols = new
    android.icu.text.DecimalFormatSymbols(locale);
    String symbol = icuSymbols.getCurrencySymbol();
    Currency currency = icuSymbols.getCurrency();
    String expectCur = null;
    String expectSym = "\u00A4";
    if(!symbol.toString().equals(expectSym) || currency != null) {
        errln("for " + locale + " expected " + expectSym+"/"+expectCur + " but got " + symbol+"/"+currency);
    } else {
        logln("for " + locale + " expected " + expectSym+"/"+expectCur + " and got " + symbol+"/"+currency);
    }
}
 
Example #2
Source File: TestMessageFormat.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestSetFormat() {
    MessageFormat ms = new MessageFormat("{number} {date}", ULocale.ENGLISH);
    final DecimalFormat decimalFormat = new DecimalFormat("000.000", DecimalFormatSymbols.getInstance(ULocale.ENGLISH));
    ms.setFormatByArgumentName("number", decimalFormat);
    final SimpleDateFormat dateFormat = new SimpleDateFormat("'year:'yy 'month:'MM 'day:'dd");
    dateFormat.setTimeZone(TimeZone.getTimeZone("Etc/GMT"));
    ms.setFormatByArgumentName("date", dateFormat);
    Map map = new HashMap();
    map.put("number", new Integer(1234));
    map.put("date", new Date(0,0,0));
    String result = ms.format(map);
    assertEquals("setFormatByArgumentName", "1234.000 year:99 month:12 day:31", result);
    Set formatNames = ms.getArgumentNames();
    assertEquals("Format Names match", formatNames, map.keySet());
    assertEquals("Decimal", decimalFormat, ms.getFormatByArgumentName("number"));
    assertEquals("Date", dateFormat, ms.getFormatByArgumentName("date"));
}
 
Example #3
Source File: CompactDecimalFormatTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private void checkCore(Map<String, String[][]> affixes, Map<String, String[]> currencyAffixes, long[] divisors, Object[][] testItems) {
    Collection<String> debugCreationErrors = new LinkedHashSet();
    CompactDecimalFormat cdf = new CompactDecimalFormat(
            "#,###.00",
            DecimalFormatSymbols.getInstance(new ULocale("fr")),
            CompactStyle.SHORT, PluralRules.createRules("one: j is 1 or f is 1"),
            divisors, affixes, currencyAffixes,
            debugCreationErrors
            );
    if (debugCreationErrors.size() != 0) {
        for (String s : debugCreationErrors) {
            errln("Creation error: " + s);
        }
    } else {
        checkCdf("special cdf ", cdf, testItems);
    }
}
 
Example #4
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * DecimalFormatSymbols should be cloned in the ctor DecimalFormat.
 * DecimalFormat(String, DecimalFormatSymbols).
 */
@Test
public void Test4087245 (){
    DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    DecimalFormat df = new DecimalFormat("#,##0.0", symbols);
    long n = 123;
    StringBuffer buf1 = new StringBuffer();
    StringBuffer buf2 = new StringBuffer();
    logln("format(" + n + ") = " +
    df.format(n, buf1, new FieldPosition(0)));
    symbols.setDecimalSeparator('p'); // change value of field
    logln("format(" + n + ") = " +
    df.format(n, buf2, new FieldPosition(0)));
    if (!buf1.toString().equals(buf2.toString()))
        errln("Test for bug 4087245 failed");
}
 
Example #5
Source File: RbnfTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestSetDecimalFormatSymbols() {
    RuleBasedNumberFormat rbnf = new RuleBasedNumberFormat(Locale.ENGLISH, RuleBasedNumberFormat.ORDINAL);

    DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.ENGLISH);

    double number = 1001;

    String[] expected = { "1,001st", "1&001st" };

    String result = rbnf.format(number);
    if (!result.equals(expected[0])) {
        errln("Format Error - Got: " + result + " Expected: " + expected[0]);
    }

    /* Set new symbol for testing */
    dfs.setGroupingSeparator('&');
    rbnf.setDecimalFormatSymbols(dfs);

    result = rbnf.format(number);
    if (!result.equals(expected[1])) {
        errln("Format Error - Got: " + result + " Expected: " + expected[1]);
    }
}
 
Example #6
Source File: BigNumberFormatTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Test the handling of the AlphaWorks BigDecimal
 */
@Test
public void TestAlphaBigDecimal() {
    DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
    /*For ICU compatibility [Richard/GCL]*/
    expect(NumberFormat.getScientificInstance(Locale.US),
           new Number[] { new android.icu.math.BigDecimal("12345.678901"),
                       },
           "1.2345678901E4");
    expect(new DecimalFormat("##0.####E0", US),
           new Number[] { new android.icu.math.BigDecimal("12345.4999"),
                          new android.icu.math.BigDecimal("12344.5001"),
                        },
           "12.345E3");
    expect(new DecimalFormat("##0.####E0", US),
           new Number[] { new android.icu.math.BigDecimal("12345.5000"),
                          new android.icu.math.BigDecimal("12346.5000"),
                        },
           "12.346E3");
}
 
Example #7
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * DecimalFormat.parse incorrectly works with a group separator.
 */
@Test
public void Test4108738()
{

    DecimalFormat df = new DecimalFormat("#,##0.###", new
    DecimalFormatSymbols(java.util.Locale.US));
    String text = "1.222,111";
    Number num = df.parse(text,new ParsePosition(0));
    if (!num.toString().equals("1.222"))
        errln("\"" + text + "\"  is parsed as " + num);
    text = "1.222x111";
    num = df.parse(text,new ParsePosition(0));
    if (!num.toString().equals("1.222"))
        errln("\"" + text + "\"  is parsed as " + num);
}
 
Example #8
Source File: BigNumberFormatTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Test the functioning of the secondary grouping value.
 */
@Test
public void TestSecondaryGrouping() {
    DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
    DecimalFormat f = new DecimalFormat("#,##,###", US);
    expect(f, new Long(123456789), "12,34,56,789");
    expectPat(f, "#,##,###");
    f.applyPattern("#,###");
    f.setSecondaryGroupingSize(4);
    expect(f, new Long(123456789), "12,3456,789");
    expectPat(f, "#,####,###");

    // On Sun JDK 1.2-1.3, the hi_IN locale uses '0' for a zero digit,
    // but on IBM JDK 1.2-1.3, the locale uses U+0966.
    f = (DecimalFormat) NumberFormat.getInstance(new Locale("hi", "IN"));
    String str = transmute("1,87,65,43,210",
                           f.getDecimalFormatSymbols().getZeroDigit());
    expect(f, new Long(1876543210), str);
}
 
Example #9
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Empty pattern produces double negative prefix.
 */
@Test
public void Test4140009() {
    final double IN[]  = {  123.456,   -123.456  };
    final String OUT[] = { "123.456", "-123.456" };
    for (int i=0; i<2; ++i) {
        DecimalFormat f = null;
        switch (i) {
        case 0:
            f = new DecimalFormat("",
                        new DecimalFormatSymbols(Locale.ENGLISH));
            break;
        case 1:
            f = new DecimalFormat("#.#",
                        new DecimalFormatSymbols(Locale.ENGLISH));
            f.applyPattern("");
            break;
        }
        for (int j=0; j<2; ++j) {
            assertEquals("<empty pat " + i + ">.format(" + IN[j] + ")",
                         OUT[j], f.format(IN[j]));
        }
    }
}
 
Example #10
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * DecimalFormat formats -0.0 as +0.0
 * See also older related bug 4106658, 4106667
 */
@Test
public void Test4147706() {
    DecimalFormat df = new DecimalFormat("#,##0.0##");
    df.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.ENGLISH));
    double d1 = -0.0;
    double d2 = -0.0001;
    StringBuffer f1 = df.format(d1, new StringBuffer(), new FieldPosition(0));
    StringBuffer f2 = df.format(d2, new StringBuffer(), new FieldPosition(0));
    if (!f1.toString().equals("-0.0")) {
        errln(d1 + " x \"" + df.toPattern() + "\" is formatted as \"" + f1 + '"');
    }
    if (!f2.toString().equals("-0.0")) {
        errln(d2 + " x \"" + df.toPattern() + "\" is formatted as \"" + f2 + '"');
    }
}
 
Example #11
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * DecimalFormat is incorrectly rounding numbers like 1.2501 to 1.2
 */
@Test
public void Test4179818() {
    String DATA[] = {
        // Input  Pattern  Expected output
        "1.2511", "#.#",   "1.3",
        "1.2501", "#.#",   "1.3",
        "0.9999", "#",     "1",
    };
    DecimalFormat fmt = new DecimalFormat("#",
            new DecimalFormatSymbols(Locale.US));
    for (int i=0; i<DATA.length; i+=3) {
        double in = Double.valueOf(DATA[i]).doubleValue();
        String pat = DATA[i+1];
        String exp = DATA[i+2];
        fmt.applyPattern(pat);
        String out = fmt.format(in);
        if (out.equals(exp)) {
            logln("Ok: " + in + " x " + pat + " = " + out);
        } else {
            errln("FAIL: " + in + " x  " + pat + " = " + out +
                  ", expected " + exp);
        }
    }
}
 
Example #12
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * API tests for API addition request A9.
 */
@Test
public void Test4061302()
{
    DecimalFormatSymbols fmt = new DecimalFormatSymbols();
    String currency = fmt.getCurrencySymbol();
    String intlCurrency = fmt.getInternationalCurrencySymbol();
    char monDecSeparator = fmt.getMonetaryDecimalSeparator();
    if (currency.equals("") ||
        intlCurrency.equals("") ||
        monDecSeparator == 0) {
        errln("getCurrencySymbols failed, got empty string.");
    }
    logln("Before set ==> Currency : " + currency + " Intl Currency : " + intlCurrency + " Monetary Decimal Separator : " + monDecSeparator);
    fmt.setCurrencySymbol("XYZ");
    fmt.setInternationalCurrencySymbol("ABC");
    fmt.setMonetaryDecimalSeparator('*');
    currency = fmt.getCurrencySymbol();
    intlCurrency = fmt.getInternationalCurrencySymbol();
    monDecSeparator = fmt.getMonetaryDecimalSeparator();
    if (!currency.equals("XYZ") ||
        !intlCurrency.equals("ABC") ||
        monDecSeparator != '*') {
        errln("setCurrencySymbols failed.");
    }
    logln("After set ==> Currency : " + currency + " Intl Currency : " + intlCurrency + " Monetary Decimal Separator : " + monDecSeparator);
}
 
Example #13
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * NumberFormat.getCurrencyInstance() produces format that uses
 * decimal separator instead of monetary decimal separator.
 *
 * Rewrote this test not to depend on the actual pattern.  Pattern should
 * never contain the monetary separator!  Decimal separator in pattern is
 * interpreted as monetary separator if currency symbol is seen!
 */
@Test
public void Test4087244 () {
    Locale de = new Locale("pt", "PT");
    DecimalFormat df = (DecimalFormat) NumberFormat.getCurrencyInstance(de);
    DecimalFormatSymbols sym = df.getDecimalFormatSymbols();
    sym.setMonetaryDecimalSeparator('$');
df.setDecimalFormatSymbols(sym);
    char decSep = sym.getDecimalSeparator();
    char monSep = sym.getMonetaryDecimalSeparator();
    //char zero = sym.getZeroDigit(); //The variable is never used
    if (decSep == monSep) {
        errln("ERROR in test: want decimal sep != monetary sep");
    } else {
        df.setMinimumIntegerDigits(1);
        df.setMinimumFractionDigits(2);
        String str = df.format(1.23);
        String monStr = "1" + monSep + "23";
        String decStr = "1" + decSep + "23";
        if (str.indexOf(monStr) >= 0 && str.indexOf(decStr) < 0) {
            logln("OK: 1.23 -> \"" + str + "\" contains \"" +
                  monStr + "\" and not \"" + decStr + '"');
        } else {
            errln("FAIL: 1.23 -> \"" + str + "\", should contain \"" +
                  monStr +
                  "\" and not \"" + decStr + '"');
        }
    }
}
 
Example #14
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * DecimalFormatSymbol.equals should always return false when
 * comparing with null.
 */

@Test
public void Test4083018 (){
    DecimalFormatSymbols dfs = new DecimalFormatSymbols();
    try {
        if (!dfs.equals(null))
            logln("Test Passed!");
    } catch (Exception foo) {
        errln("Test for bug 4083018 failed => Message : " + foo.getMessage());
    }
}
 
Example #15
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 4233840: NumberFormat does not round correctly
 */
@Test
public void test4233840() {
    float f = 0.0099f;

    NumberFormat nf = new DecimalFormat("0.##", new DecimalFormatSymbols(Locale.US));
nf.setMinimumFractionDigits(2);

String result = nf.format(f);

if (!result.equals("0.01")) {
    errln("FAIL: input: " + f + ", expected: 0.01, got: " + result);
}
}
 
Example #16
Source File: PluralFormatUnitTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestDecimals() {
    // Simple number replacement.
    PluralFormat pf = new PluralFormat(ULocale.ENGLISH, "one{one meter}other{# meters}");
    assertEquals("simple format(1)", "one meter", pf.format(1));
    assertEquals("simple format(1.5)", "1.5 meters", pf.format(1.5));
    PluralFormat pf2 = new PluralFormat(ULocale.ENGLISH,
            "offset:1 one{another meter}other{another # meters}");
    pf2.setNumberFormat(new DecimalFormat("0.0", new DecimalFormatSymbols(ULocale.ENGLISH)));
    assertEquals("offset-decimals format(1)", "another 0.0 meters", pf2.format(1));
    assertEquals("offset-decimals format(2)", "another 1.0 meters", pf2.format(2));
    assertEquals("offset-decimals format(2.5)", "another 1.5 meters", pf2.format(2.5));
}
 
Example #17
Source File: IntlTestDecimalFormatSymbols.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void testDigitSymbols() {
    final char defZero = '0';
    final char[] defDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    final String[] defDigitStrings = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
    final String[] osmanyaDigitStrings = {
        "\uD801\uDCA0", "\uD801\uDCA1", "\uD801\uDCA2", "\uD801\uDCA3", "\uD801\uDCA4",
        "\uD801\uDCA5", "\uD801\uDCA6", "\uD801\uDCA7", "\uD801\uDCA8", "\uD801\uDCA9"
    };

    DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.ENGLISH);

    symbols.setDigitStrings(osmanyaDigitStrings);
    if (!Arrays.equals(symbols.getDigitStrings(), osmanyaDigitStrings)) {
        errln("ERROR: Osmanya digits (supplementary) should be set");
    }
    if (defZero != symbols.getZeroDigit()) {
        errln("ERROR: Zero digit should be 0");
    }
    if (!Arrays.equals(symbols.getDigits(), defDigits)) {
        errln("ERROR: Char digits should be Latin digits");
    }

    // Reset digits to Latin
    symbols.setZeroDigit(defZero);
    if (!Arrays.equals(symbols.getDigitStrings(), defDigitStrings)) {
        errln("ERROR: Latin digits should be set" + symbols.getDigitStrings()[0]);
    }
}
 
Example #18
Source File: IntlTestDecimalFormatSymbols.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void testCoverage() {
    DecimalFormatSymbols df = new DecimalFormatSymbols();
    DecimalFormatSymbols df2 = (DecimalFormatSymbols)df.clone();
    if (!df.equals(df2) || df.hashCode() != df2.hashCode()) {
        errln("decimal format symbols clone, equals, or hashCode failed");
    }
}
 
Example #19
Source File: ZygoteInit.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private static void beginIcuCachePinning() {
    // Pin ICU data in memory from this point that would normally be held by soft references.
    // Without this, any references created immediately below or during class preloading
    // would be collected when the Zygote GC runs in gcAndFinalize().
    Log.i(TAG, "Installing ICU cache reference pinning...");

    CacheValue.setStrength(CacheValue.Strength.STRONG);

    Log.i(TAG, "Preloading ICU data...");
    // Explicitly exercise code to cache data apps are likely to need.
    ULocale[] localesToPin = { ULocale.ROOT, ULocale.US, ULocale.getDefault() };
    for (ULocale uLocale : localesToPin) {
        new DecimalFormatSymbols(uLocale);
    }
}
 
Example #20
Source File: RbnfTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestVariableDecimalPoint() {
    String enRules = "%spellout-numbering:"
            + "-x: minus >>;"
            + "x.x: << point >>;"
            + "x,x: << comma >>;"
            + "0.x: xpoint >>;"
            + "0,x: xcomma >>;"
            + "0: zero;"
            + "1: one;"
            + "2: two;"
            + "3: three;"
            + "4: four;"
            + "5: five;"
            + "6: six;"
            + "7: seven;"
            + "8: eight;"
            + "9: nine;";
    RuleBasedNumberFormat enFormatter = new RuleBasedNumberFormat(enRules, ULocale.ENGLISH);
    String[][] enTestPointData = {
            {"1.1", "one point one"},
            {"1.23", "one point two three"},
            {"0.4", "xpoint four"},
    };
    doTest(enFormatter, enTestPointData, true);
    DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(ULocale.ENGLISH);
    decimalFormatSymbols.setDecimalSeparator(',');
    enFormatter.setDecimalFormatSymbols(decimalFormatSymbols);
    String[][] enTestCommaData = {
            {"1.1", "one comma one"},
            {"1.23", "one comma two three"},
            {"0.4", "xcomma four"},
    };
    doTest(enFormatter, enTestCommaData, true);
}
 
Example #21
Source File: BigNumberFormatTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 */
@Test
public void TestPad() {
    DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
    expect(new DecimalFormat("*^##.##", US),
           new Object[] { new Long(0),      "^^^^0",
                          new Double(-1.3), "^-1.3",
                        }
           );
    expect(new DecimalFormat("##0.0####E0*_ 'g-m/s^2'", US),
           new Object[] { new Long(0),       "0.0E0______ g-m/s^2",
                          new Double(1.0/3), "333.333E-3_ g-m/s^2",
                        }
           );
    expect(new DecimalFormat("##0.0####*_ 'g-m/s^2'", US),
           new Object[] { new Long(0),       "0.0______ g-m/s^2",
                          new Double(1.0/3), "0.33333__ g-m/s^2",
                        }
           );
    expect(new DecimalFormat("*x#,###,###,##0.00;*x(#,###,###,##0.00)", US),
           new Object[] {
               new Long(-100),        "xxxxxxxx(100.00)",
               new Long(-1000),       "xxxxxx(1,000.00)",
               new Long(-1000000),    "xx(1,000,000.00)",
               new Long(-1000000000), "(1,000,000,000.00)",
           });
}
 
Example #22
Source File: BigNumberFormatTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestExponent() {
    DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
    DecimalFormat fmt1 = new DecimalFormat("0.###E0", US);
    DecimalFormat fmt2 = new DecimalFormat("0.###E+0", US);
    Number n = new Long(1234);
    expect(fmt1, n, "1.234E3");
    expect(fmt2, n, "1.234E+3");
    expect(fmt1, "1.234E3", n);
    expect(fmt1, "1.234E+3", n); // Either format should parse "E+3"
    expect(fmt2, "1.234E+3", n);
}
 
Example #23
Source File: IntlTestDecimalFormatSymbolsC.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/** helper functions**/
public void verify(double value, String pattern, DecimalFormatSymbols sym, String expected) {
    DecimalFormat df = new DecimalFormat(pattern, sym);
    StringBuffer buffer = new StringBuffer("");
    FieldPosition pos = new FieldPosition(-1);
    buffer = df.format(value, buffer, pos);
    if(!buffer.toString().equals(expected)){
        errln("ERROR: format failed after setSymbols()\n Expected" + 
            expected + ", Got " + buffer);
    }
}
 
Example #24
Source File: TimePickerClockDelegate.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Updates hour formatting based on the current locale and 24-hour mode.
 * <p>
 * Determines how the hour should be formatted, sets member variables for
 * leading zero and starting hour, and sets the hour view's presentation.
 */
private void updateHourFormat() {
    final String bestDateTimePattern = DateFormat.getBestDateTimePattern(
            mLocale, mIs24Hour ? "Hm" : "hm");
    final int lengthPattern = bestDateTimePattern.length();
    boolean showLeadingZero = false;
    char hourFormat = '\0';

    for (int i = 0; i < lengthPattern; i++) {
        final char c = bestDateTimePattern.charAt(i);
        if (c == 'H' || c == 'h' || c == 'K' || c == 'k') {
            hourFormat = c;
            if (i + 1 < lengthPattern && c == bestDateTimePattern.charAt(i + 1)) {
                showLeadingZero = true;
            }
            break;
        }
    }

    mHourFormatShowLeadingZero = showLeadingZero;
    mHourFormatStartsAtZero = hourFormat == 'K' || hourFormat == 'H';

    // Update hour text field.
    final int minHour = mHourFormatStartsAtZero ? 0 : 1;
    final int maxHour = (mIs24Hour ? 23 : 11) + minHour;
    mHourView.setRange(minHour, maxHour);
    mHourView.setShowLeadingZeroes(mHourFormatShowLeadingZero);

    final String[] digits = DecimalFormatSymbols.getInstance(mLocale).getDigitStrings();
    int maxCharLength = 0;
    for (int i = 0; i < 10; i++) {
        maxCharLength = Math.max(maxCharLength, digits[i].length());
    }
    mTextInputPickerView.setHourFormat(maxCharLength * 2);
}
 
Example #25
Source File: NumberKeyListener.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Nullable
static boolean addDigits(@NonNull Collection<Character> collection, @Nullable Locale locale) {
    if (locale == null) {
        return false;
    }
    final String[] digits = DecimalFormatSymbols.getInstance(locale).getDigitStrings();
    for (int i = 0; i < 10; i++) {
        if (digits[i].length() > 1) { // multi-codeunit digits. Not supported.
            return false;
        }
        collection.add(Character.valueOf(digits[i].charAt(0)));
    }
    return true;
}
 
Example #26
Source File: NumberFormatSpecificationTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private static String formatFrWithPattern(double d, String pattern) {
    DecimalFormatSymbols sym = new DecimalFormatSymbols(ULocale.FRANCE);
    DecimalFormat fmt = new DecimalFormat(pattern, sym);
    return fmt.format(d).replace('\u00a0', ' ');
}
 
Example #27
Source File: NumberFormatSpecificationTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private static NumberFormat nfWithPattern(String pattern) {
    DecimalFormatSymbols sym = new DecimalFormatSymbols(ULocale.FRANCE);
    return new DecimalFormat(pattern, sym);
}
 
Example #28
Source File: NumberFormatRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestJB5509() {
    String[] data = {
            "1,2",
            "1.2",
            "1,2.5",
            "1,23.5",
            "1,234.5",
            "1,234",
            "1,234,567",
            "1,234,567.8",
            "1,234,5",
            "1,234,5.6",
            "1,234,56.7"
    };
    boolean[] expected = { // false for expected parse failure
            false,
            true,
            false,
            false,
            true,
            true,
            true,
            true,
            false,
            false,
            false,
            false
    };

    DecimalFormat df = new DecimalFormat("#,##0.###", new DecimalFormatSymbols(new ULocale("en_US")));
    df.setParseStrict(true);
    for (int i = 0; i < data.length; i++) {
        try {
            df.parse(data[i]);
            if (!expected[i]) {
                errln("Failed: ParseException must be thrown for string " + data[i]);
            }
        } catch (ParseException pe) {
            if (expected[i]) {
                errln("Failed: ParseException must not be thrown for string " + data[i]);
            }
        }
    }
}
 
Example #29
Source File: BigNumberFormatTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 */
@Test
public void TestScientific() {
    DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
    /*For ICU compatibility [Richard/GCL]*/
    expect(NumberFormat.getScientificInstance(Locale.US),
           new Number[] { new Double(12345.678901),
                          new java.math.BigDecimal("12345.678901"),
                        },
           "1.2345678901E4");
    expect(new DecimalFormat("##0.###E0", US),
           new Double(12345),
           "12.34E3");
    expect(new DecimalFormat("##0.###E0", US),
           new Double(12345.00001),
           "12.35E3");
    expect(new DecimalFormat("##0.####E0", US),
           new Number[] { new Integer(12345),
                          new Long(12345),
                          new java.math.BigDecimal("12345.4999"),
                          new java.math.BigDecimal("12344.5001"),
                        },
           "12.345E3");
    expect(new DecimalFormat("##0.####E0", US),
           new Number[] { new java.math.BigDecimal("12345.5000"),
                          new java.math.BigDecimal("12346.5000"),
                        },
           "12.346E3");
    /*For ICU compatibility [Richard/GCL]*/
    expect(NumberFormat.getScientificInstance(Locale.FRANCE),
           new Double(12345.678901),
           "1,2345678901E4");
    expect(new DecimalFormat("##0.####E0", US),
           new Double(789.12345e-9),
           "789.12E-9");
    expect(new DecimalFormat("##0.####E0", US),
           new Double(780.e-9),
           "780E-9");
    expect(new DecimalFormat(".###E0", US),
           new Double(45678),
           ".457E5");
    expect(new DecimalFormat(".###E0", US),
           new Long(0),
           ".0E0");
    expect(new DecimalFormat[] { new DecimalFormat("#E0", US),
                                 new DecimalFormat("##E0", US),
                                 new DecimalFormat("####E0", US),
                                 new DecimalFormat("0E0", US),    
                                 new DecimalFormat("00E0", US),   
                                 new DecimalFormat("000E0", US), 
                               },
           new Long(45678000),
           new String[] { "4.5678E7",
                          "45.678E6",
                          "4567.8E4",
                          "5E7",
                          "46E6",  
                          "457E5",
                        }
           );
    expect(new DecimalFormat("###E0", US),
           new Object[] { new Double(0.0000123), "12.3E-6",
                          new Double(0.000123), "123E-6",
                          new java.math.BigDecimal("0.00123"), "1.23E-3", // Cafe VM messes up Double(0.00123)
                          new Double(0.0123), "12.3E-3",
                          new Double(0.123), "123E-3",
                          new Double(1.23), "1.23E0",
                          new Double(12.3), "12.3E0",
                          new Double(123), "123E0",
                          new Double(1230), "1.23E3",
                         });
    expect(new DecimalFormat("0.#E+00", US),
           new Object[] { new Double(0.00012), "1.2E-04",
                          new Long(12000),     "1.2E+04",
                         });
}
 
Example #30
Source File: BigNumberFormatTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 */
@Test
public void TestPatterns() {
    DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
    DecimalFormat fmt = new DecimalFormat("#", US);

    expectPad(fmt, "*^#", DecimalFormat.PAD_BEFORE_PREFIX, 1, '^');
    expectPad(fmt, "$*^#", DecimalFormat.PAD_AFTER_PREFIX, 2, '^');
    expectPad(fmt, "#*^", DecimalFormat.PAD_BEFORE_SUFFIX, 1, '^');
    expectPad(fmt, "#$*^", DecimalFormat.PAD_AFTER_SUFFIX, 2, '^');
    expectPad(fmt, "$*^$#", ILLEGAL);
    expectPad(fmt, "#$*^$", ILLEGAL);
    expectPad(fmt, "'pre'#,##0*x'post'", DecimalFormat.PAD_BEFORE_SUFFIX,
              12, 'x');
    expectPad(fmt, "''#0*x", DecimalFormat.PAD_BEFORE_SUFFIX,
              3, 'x');
    expectPad(fmt, "'I''ll'*a###.##", DecimalFormat.PAD_AFTER_PREFIX,
              10, 'a');

    fmt.applyPattern("AA#,##0.00ZZ");
    fmt.setPadCharacter('^');

    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
    expectPat(fmt, "AA*^####,##0.00ZZ"); // This is the interesting case

    fmt.setFormatWidth(16);
    //              12  34567890123456
    expectPat(fmt, "AA*^#,###,##0.00ZZ");
}