use of android.icu.text.NumberFormat in project j2objc by google.
the class MyNumberFormat method Test4071492.
/**
* DecimalFormat does not round up correctly.
*/
@Test
public void Test4071492() {
double x = 0.00159999;
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(4);
String out = nf.format(x);
logln("0.00159999 formats with 4 fractional digits to " + out);
String expected = "0.0016";
if (!out.equals(expected))
errln("FAIL: Expected " + expected);
}
use of android.icu.text.NumberFormat in project j2objc by google.
the class MyNumberFormat method Test4086575.
/**
* A space as a group separator for localized pattern causes
* wrong format. WorkAround : use non-breaking space.
*/
@Test
public void Test4086575() {
NumberFormat nf = NumberFormat.getInstance(Locale.FRANCE);
logln("nf toPattern1: " + ((DecimalFormat) nf).toPattern());
logln("nf toLocPattern1: " + ((DecimalFormat) nf).toLocalizedPattern());
// No group separator
logln("...applyLocalizedPattern ###,00;(###,00) ");
((DecimalFormat) nf).applyLocalizedPattern("###,00;(###,00)");
logln("nf toPattern2: " + ((DecimalFormat) nf).toPattern());
logln("nf toLocPattern2: " + ((DecimalFormat) nf).toLocalizedPattern());
// 1234,00
logln("nf: " + nf.format(1234));
// (1234,00)
logln("nf: " + nf.format(-1234));
// Space as group separator
logln("...applyLocalizedPattern # ###,00;(# ###,00) ");
((DecimalFormat) nf).applyLocalizedPattern("#\u00a0###,00;(#\u00a0###,00)");
logln("nf toPattern2: " + ((DecimalFormat) nf).toPattern());
logln("nf toLocPattern2: " + ((DecimalFormat) nf).toLocalizedPattern());
String buffer = nf.format(1234);
if (!buffer.equals("1\u00a0234,00"))
// Expect 1 234,00
errln("nf : " + buffer);
buffer = nf.format(-1234);
if (!buffer.equals("(1\u00a0234,00)"))
// Expect (1 234,00)
errln("nf : " + buffer);
// Erroneously prints:
// 1234,00 ,
// (1234,00 ,)
}
use of android.icu.text.NumberFormat in project j2objc by google.
the class MyNumberFormat method Test4167494.
/**
* NumberFormat truncates data
*/
@Test
public void Test4167494() throws Exception {
NumberFormat fmt = NumberFormat.getInstance(Locale.US);
double a = Double.MAX_VALUE;
String s = fmt.format(a);
double b = fmt.parse(s).doubleValue();
boolean match = a == b;
if (match) {
logln("" + a + " -> \"" + s + "\" -> " + b + " ok");
} else {
errln("" + a + " -> \"" + s + "\" -> " + b + " FAIL");
}
// in case we want to address this later.
if (false) {
a = Double.MIN_VALUE;
s = fmt.format(a);
b = fmt.parse(s).doubleValue();
match = a == b;
if (match) {
logln("" + a + " -> \"" + s + "\" -> " + b + " ok");
} else {
errln("" + a + " -> \"" + s + "\" -> " + b + " FAIL");
}
}
}
use of android.icu.text.NumberFormat in project j2objc by google.
the class MyNumberFormat method Test4071005.
/**
* Data rounding errors for French (Canada) locale
*/
@Test
public void Test4071005() {
NumberFormat formatter;
String tempString;
/* user error :
String expectedDefault = "-5 789,987";
String expectedCurrency = "5 789,98\u00a0$";
String expectedPercent = "-578 998%";
*/
String expectedDefault = "-5\u00a0789,988";
String expectedCurrency = "5\u00a0789,99\u00a0$";
String expectedPercent = "-578\u00a0999\u00A0%";
formatter = NumberFormat.getNumberInstance(Locale.CANADA_FRENCH);
tempString = formatter.format(-5789.9876);
if (tempString.equals(expectedDefault)) {
logln("Bug 4071005 default test passed.");
} else {
errln("Failed:" + " Expected " + expectedDefault + " Received " + tempString);
}
formatter = NumberFormat.getCurrencyInstance(Locale.CANADA_FRENCH);
tempString = formatter.format(5789.9876);
if (tempString.equals(expectedCurrency)) {
logln("Bug 4071005 currency test passed.");
} else {
errln("Failed:" + " Expected " + expectedCurrency + " Received " + tempString);
}
formatter = NumberFormat.getPercentInstance(Locale.CANADA_FRENCH);
tempString = formatter.format(-5789.9876);
if (tempString.equals(expectedPercent)) {
logln("Bug 4071005 percentage test passed.");
} else {
errln("Failed:" + " Expected " + expectedPercent + " Received " + tempString);
}
}
use of android.icu.text.NumberFormat in project j2objc by google.
the class PluralFormatUnitTest method TestConstructor.
@Test
public void TestConstructor() {
// Test correct formatting of numbers.
PluralFormat[] plFmts = new PluralFormat[10];
plFmts[0] = new PluralFormat();
plFmts[0].applyPattern("other{#}");
plFmts[1] = new PluralFormat(PluralRules.DEFAULT);
plFmts[1].applyPattern("other{#}");
plFmts[2] = new PluralFormat(PluralRules.DEFAULT, "other{#}");
plFmts[3] = new PluralFormat("other{#}");
plFmts[4] = new PluralFormat(ULocale.getDefault());
plFmts[4].applyPattern("other{#}");
plFmts[5] = new PluralFormat(ULocale.getDefault(), PluralRules.DEFAULT);
plFmts[5].applyPattern("other{#}");
plFmts[6] = new PluralFormat(ULocale.getDefault(), PluralRules.DEFAULT, "other{#}");
plFmts[7] = new PluralFormat(ULocale.getDefault(), "other{#}");
// Constructors with Java Locale
plFmts[8] = new PluralFormat(Locale.getDefault());
plFmts[8].applyPattern("other{#}");
plFmts[9] = new PluralFormat(Locale.getDefault(), PluralRules.DEFAULT);
plFmts[9].applyPattern("other{#}");
// These plural formats should produce the same output as a
// NumberFormat for the default locale.
NumberFormat numberFmt = NumberFormat.getInstance(ULocale.getDefault());
for (int n = 1; n < 13; n++) {
String result = numberFmt.format(n);
for (int k = 0; k < plFmts.length; ++k) {
TestFmwk.assertEquals("PluralFormat's output is not as expected", result, plFmts[k].format(n));
}
}
// Test some bigger numbers.
// Coverage: Use the format(Object, ...) version.
StringBuffer sb = new StringBuffer();
FieldPosition ignore = new FieldPosition(-1);
for (int n = 100; n < 113; n++) {
String result = numberFmt.format(n * n);
for (int k = 0; k < plFmts.length; ++k) {
sb.delete(0, sb.length());
String pfResult = plFmts[k].format(Long.valueOf(n * n), sb, ignore).toString();
TestFmwk.assertEquals("PluralFormat's output is not as expected", result, pfResult);
}
}
}
Aggregations