use of android.icu.text.DecimalFormat in project android_packages_apps_Settings by SudaMod.
the class FileSizeFormatter method getNumberFormatter.
private static NumberFormat getNumberFormatter(Locale locale, int fractionDigits) {
final NumberFormat numberFormatter = NumberFormat.getInstance(locale);
numberFormatter.setMinimumFractionDigits(fractionDigits);
numberFormatter.setMaximumFractionDigits(fractionDigits);
numberFormatter.setGroupingUsed(false);
if (numberFormatter instanceof DecimalFormat) {
// We do this only for DecimalFormat, since in the general NumberFormat case, calling
// setRoundingMode may throw an exception.
numberFormatter.setRoundingMode(BigDecimal.ROUND_HALF_UP);
}
return numberFormatter;
}
use of android.icu.text.DecimalFormat in project j2objc by google.
the class NumberFormatTest method TestCurrencyPlurals.
@Test
public void TestCurrencyPlurals() {
String[][] tests = { { "en", "USD", "1", "1 US dollar" }, { "en", "USD", "1.0", "1.0 US dollars" }, { "en", "USD", "1.00", "1.00 US dollars" }, { "en", "USD", "1.99", "1.99 US dollars" }, { "en", "AUD", "1", "1 Australian dollar" }, { "en", "AUD", "1.00", "1.00 Australian dollars" }, { "sl", "USD", "1", "1 ameri\u0161ki dolar" }, { "sl", "USD", "2", "2 ameri\u0161ka dolarja" }, { "sl", "USD", "3", "3 ameri\u0161ki dolarji" }, { "sl", "USD", "5", "5 ameriških dolarjev" }, { "fr", "USD", "1.99", "1,99 dollar des États-Unis" }, { "ru", "RUB", "1", "1 \u0440\u043E\u0441\u0441\u0438\u0439\u0441\u043A\u0438\u0439 \u0440\u0443\u0431\u043B\u044C" }, { "ru", "RUB", "2", "2 \u0440\u043E\u0441\u0441\u0438\u0439\u0441\u043A\u0438\u0445 \u0440\u0443\u0431\u043B\u044F" }, { "ru", "RUB", "5", "5 \u0440\u043E\u0441\u0441\u0438\u0439\u0441\u043A\u0438\u0445 \u0440\u0443\u0431\u043B\u0435\u0439" } };
for (String[] test : tests) {
DecimalFormat numberFormat = (DecimalFormat) DecimalFormat.getInstance(new ULocale(test[0]), NumberFormat.PLURALCURRENCYSTYLE);
numberFormat.setCurrency(Currency.getInstance(test[1]));
double number = Double.parseDouble(test[2]);
int dotPos = test[2].indexOf('.');
int decimals = dotPos < 0 ? 0 : test[2].length() - dotPos - 1;
int digits = dotPos < 0 ? test[2].length() : test[2].length() - 1;
numberFormat.setMaximumFractionDigits(decimals);
numberFormat.setMinimumFractionDigits(decimals);
String actual = numberFormat.format(number);
assertEquals(test[0] + "\t" + test[1] + "\t" + test[2], test[3], actual);
numberFormat.setMaximumSignificantDigits(digits);
numberFormat.setMinimumSignificantDigits(digits);
actual = numberFormat.format(number);
assertEquals(test[0] + "\t" + test[1] + "\t" + test[2], test[3], actual);
}
}
use of android.icu.text.DecimalFormat in project j2objc by google.
the class NumberFormatTest method TestNonpositiveMultiplier.
@Test
public void TestNonpositiveMultiplier() {
DecimalFormat df = new DecimalFormat("0");
try {
df.setMultiplier(0);
// bad
errln("DecimalFormat.setMultiplier(0) did not throw an IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// good
}
try {
df.setMultiplier(-1);
if (df.getMultiplier() != -1) {
errln("DecimalFormat.setMultiplier(-1) did not change the multiplier to -1");
return;
}
// good
} catch (IllegalArgumentException ex) {
// bad
errln("DecimalFormat.setMultiplier(-1) threw an IllegalArgumentException");
return;
}
expect(df, "1122.123", -1122.123);
expect(df, "-1122.123", 1122.123);
expect(df, "1.2", -1.2);
expect(df, "-1.2", 1.2);
expect2(df, Long.MAX_VALUE, BigInteger.valueOf(Long.MAX_VALUE).negate().toString());
expect2(df, Long.MIN_VALUE, BigInteger.valueOf(Long.MIN_VALUE).negate().toString());
expect2(df, Long.MAX_VALUE / 2, BigInteger.valueOf(Long.MAX_VALUE / 2).negate().toString());
expect2(df, Long.MIN_VALUE / 2, BigInteger.valueOf(Long.MIN_VALUE / 2).negate().toString());
expect2(df, BigDecimal.valueOf(Long.MAX_VALUE), BigDecimal.valueOf(Long.MAX_VALUE).negate().toString());
expect2(df, BigDecimal.valueOf(Long.MIN_VALUE), BigDecimal.valueOf(Long.MIN_VALUE).negate().toString());
expect2(df, java.math.BigDecimal.valueOf(Long.MAX_VALUE), java.math.BigDecimal.valueOf(Long.MAX_VALUE).negate().toString());
expect2(df, java.math.BigDecimal.valueOf(Long.MIN_VALUE), java.math.BigDecimal.valueOf(Long.MIN_VALUE).negate().toString());
}
use of android.icu.text.DecimalFormat in project j2objc by google.
the class NumberFormatTest method TestPerMill.
@Test
public void TestPerMill() {
DecimalFormat fmt = new DecimalFormat("###.###\u2030");
assertEquals("0.4857 x ###.###\u2030", "485.7\u2030", fmt.format(0.4857));
DecimalFormatSymbols sym = new DecimalFormatSymbols(Locale.ENGLISH);
sym.setPerMill('m');
DecimalFormat fmt2 = new DecimalFormat("", sym);
fmt2.applyLocalizedPattern("###.###m");
assertEquals("0.4857 x ###.###m", "485.7m", fmt2.format(0.4857));
}
use of android.icu.text.DecimalFormat in project j2objc by google.
the class NumberFormatTest method TestLenientSymbolParsing.
/*
* Testing lenient decimal/grouping separator parsing
*/
@Test
public void TestLenientSymbolParsing() {
DecimalFormat fmt = new DecimalFormat();
DecimalFormatSymbols sym = new DecimalFormatSymbols();
expect(fmt, "12\u300234", 12.34);
// Ticket#7345 - case 1
// Even strict parsing, the decimal separator set in the symbols
// should be successfully parsed.
sym.setDecimalSeparator('\u3002');
// non-strict
fmt.setDecimalFormatSymbols(sym);
// strict - failed before the fix for #7345
fmt.setParseStrict(true);
expect(fmt, "23\u300245", 23.45);
fmt.setParseStrict(false);
// Ticket#7345 - case 2
// Decimal separator variants other than DecimalFormatSymbols.decimalSeparator
// should not hide the grouping separator DecimalFormatSymbols.groupingSeparator.
sym.setDecimalSeparator('.');
sym.setGroupingSeparator(',');
fmt.setDecimalFormatSymbols(sym);
expect(fmt, "1,234.56", 1234.56);
sym.setGroupingSeparator('\uFF61');
fmt.setDecimalFormatSymbols(sym);
expect(fmt, "2\uFF61345.67", 2345.67);
// Ticket#7128
//
sym.setGroupingSeparator(',');
fmt.setDecimalFormatSymbols(sym);
String skipExtSepParse = ICUConfig.get("android.icu.text.DecimalFormat.SkipExtendedSeparatorParsing", "false");
if (skipExtSepParse.equals("true")) {
// When the property SkipExtendedSeparatorParsing is true,
// DecimalFormat does not use the extended equivalent separator
// data and only uses the one in DecimalFormatSymbols.
expect(fmt, "23 456", 23);
} else {
// Lenient separator parsing is enabled by default.
// A space character below is interpreted as a
// group separator, even ',' is used as grouping
// separator in the symbols.
expect(fmt, "12 345", 12345);
}
}
Aggregations