Search in sources :

Example 26 with DecimalFormat

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

the class NumberFormatTest method TestStrictParse.

@Test
public void TestStrictParse() {
    String[] pass = { // single zero before end of text is not leading
    "0", // single zero at end of number is not leading
    "0 ", // single zero before period (or decimal, it's ambiguous) is not leading
    "0.", // single zero before comma (not group separator) is not leading
    "0,", // single zero before decimal followed by digit is not leading
    "0.0", // same as above before period (or decimal) is not leading
    "0. ", // comma stops parse of decimal (no grouping)
    "0.100,5", // leading decimal is ok, even with zeros
    ".00", // group separators are not required
    "1234567", // comma not followed by digit is not a group separator, but end of number
    "12345, ", // if group separator is present, group sizes must be appropriate
    "1,234, ", // ...secondary too
    "1,234,567", // an exponent not followed by zero or digits is not an exponent
    "0E", // leading zero before zero - used to be error - see ticket #7913
    "00", // leading zero before digit - used to be error - see ticket #7913
    "012", // leading zero before group separator - used to be error - see ticket #7913
    "0,456" };
    String[] fail = { // wrong number of digits after group separator
    "1,2", // leading group separator before zero
    ",0", // leading group separator before digit
    ",1", // leading group separator before decimal
    ",.02", // group separator before decimal
    "1,.02", // multiple group separators
    "1,,200", // wrong number of digits in primary group
    "1,45", // wrong number of digits in primary group
    "1,45 that", // wrong number of digits in primary group
    "1,45.34", // wrong number of digits in secondary group
    "1234,567", // wrong number of digits in secondary group
    "12,34,567", // wrong number of digits in primary and secondary groups
    "1,23,456,7890" };
    DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(Locale.ENGLISH);
    runStrictParseBatch(nf, pass, fail);
    String[] scientificPass = { // single zero before exponent is ok
    "0E2", // any number of digits before exponent is ok
    "1234E2", // an exponent string not followed by zero or digits is not an exponent
    "1,234E", // leading zeroes now allowed in strict mode - see ticket #
    "00E2" };
    String[] scientificFail = { // group separators with exponent fail
    "1,234E2" };
    nf = (DecimalFormat) NumberFormat.getInstance(Locale.ENGLISH);
    runStrictParseBatch(nf, scientificPass, scientificFail);
    String[] mixedPass = { "12,34,567", "12,34,567,", "12,34,567, that", "12,34,567 that" };
    String[] mixedFail = { "12,34,56", "12,34,56,", "12,34,56, that ", "12,34,56 that" };
    nf = new DecimalFormat("#,##,##0.#");
    runStrictParseBatch(nf, mixedPass, mixedFail);
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 27 with DecimalFormat

use of android.icu.text.DecimalFormat 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 28 with DecimalFormat

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

the class NumberFormatTest method TestCurrFmtNegSameAsPositive.

@Test
public void TestCurrFmtNegSameAsPositive() {
    DecimalFormatSymbols decfmtsym = DecimalFormatSymbols.getInstance(Locale.US);
    // ZERO WIDTH SPACE, in ICU4J cannot set to empty string
    decfmtsym.setMinusSign('\u200B');
    DecimalFormat decfmt = new DecimalFormat("\u00A4#,##0.00;\u00A4#,##0.00", decfmtsym);
    String currFmtResult = decfmt.format(-100.0);
    if (!currFmtResult.equals("\u200B$100.00")) {
        errln("decfmt.toPattern results wrong, expected \u200B$100.00, got " + currFmtResult);
    }
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 29 with DecimalFormat

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

the class NumberFormatTest method TestDecimalFormatCurrencyParse.

@Test
public void TestDecimalFormatCurrencyParse() {
    // Locale.US
    DecimalFormatSymbols sym = new DecimalFormatSymbols(Locale.US);
    StringBuffer pat = new StringBuffer("");
    char currency = 0x00A4;
    // "\xA4#,##0.00;-\xA4#,##0.00"
    pat.append(currency).append(currency).append(currency).append("#,##0.00;-").append(currency).append(currency).append(currency).append("#,##0.00");
    DecimalFormat fmt = new DecimalFormat(pat.toString(), sym);
    String[][] DATA = { // string to be parsed, the parsed result (number)
    { "$1.00", "1" }, { "USD1.00", "1" }, { "1.00 US dollar", "1" }, { "$1,234.56", "1234.56" }, { "USD1,234.56", "1234.56" }, { "1,234.56 US dollar", "1234.56" } };
    try {
        for (int i = 0; i < DATA.length; ++i) {
            String stringToBeParsed = DATA[i][0];
            double parsedResult = Double.parseDouble(DATA[i][1]);
            Number num = fmt.parse(stringToBeParsed);
            if (num.doubleValue() != parsedResult) {
                errln("FAIL parse: Expected " + parsedResult);
            }
        }
    } catch (ParseException e) {
        errln("FAILED, DecimalFormat parse currency: " + e.toString());
    }
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 30 with DecimalFormat

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

the class NumberFormatTest method TestParseCurrPatternWithDecStyle.

@Test
public void TestParseCurrPatternWithDecStyle() {
    String currpat = "ยค#,##0.00";
    String parsetxt = "x0y$";
    DecimalFormat decfmt = (DecimalFormat) NumberFormat.getInstance(new ULocale("en_US"), NumberFormat.NUMBERSTYLE);
    decfmt.applyPattern(currpat);
    ParsePosition ppos = new ParsePosition(0);
    Number value = decfmt.parse(parsetxt, ppos);
    if (ppos.getIndex() != 0) {
        errln("DecimalFormat.parse expected to fail but got ppos " + ppos.getIndex() + ", value " + value);
    }
}
Also used : ULocale(android.icu.util.ULocale) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) ParsePosition(java.text.ParsePosition) 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