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);
}
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));
}
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);
}
}
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());
}
}
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);
}
}
Aggregations