use of android.icu.text.DecimalFormat in project j2objc by google.
the class MyNumberFormat method Test4243011.
/**
* 4243011: Formatting .5 rounds to "1" instead of "0"
*/
@Test
public void Test4243011() {
double[] DATA = { 0.5, 1.5, 2.5, 3.5, 4.5 };
String[] EXPECTED = { "0.", "2.", "2.", "4.", "4." };
DecimalFormat format = new DecimalFormat("0.");
for (int i = 0; i < DATA.length; i++) {
String result = format.format(DATA[i]);
if (result.equals(EXPECTED[i])) {
logln("OK: got " + result);
} else {
errln("FAIL: got " + result);
}
}
}
use of android.icu.text.DecimalFormat in project j2objc by google.
the class MyNumberFormat method Test4106664.
/**
* DecimalFormat.format(long n) fails if n * multiplier > MAX_LONG.
*/
@Test
public void Test4106664() {
DecimalFormat df = new DecimalFormat();
long n = 1234567890123456L;
int m = 12345678;
BigInteger bigN = BigInteger.valueOf(n);
bigN = bigN.multiply(BigInteger.valueOf(m));
df.setMultiplier(m);
df.setGroupingUsed(false);
logln("formated: " + df.format(n, new StringBuffer(), new FieldPosition(0)));
logln("expected: " + bigN.toString());
}
use of android.icu.text.DecimalFormat in project j2objc by google.
the class MyNumberFormat method Test4179818.
/**
* 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);
}
}
}
use of android.icu.text.DecimalFormat in project j2objc by google.
the class MyNumberFormat method Test4122840.
/**
* Locale data should use generic currency symbol
*
* 1) Make sure that all currency formats use the generic currency symbol.
* 2) Make sure we get the same results using the generic symbol or a
* hard-coded one.
*/
@Test
public void Test4122840() {
Locale[] locales = NumberFormat.getAvailableLocales();
for (int i = 0; i < locales.length; i++) {
ICUResourceBundle rb = (ICUResourceBundle) ICUResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, locales[i]);
//
// Get the currency pattern for this locale. We have to fish it
// out of the ResourceBundle directly, since DecimalFormat.toPattern
// will return the localized symbol, not \00a4
//
String pattern = rb.getStringWithFallback("NumberElements/latn/patterns/currencyFormat");
if (pattern.indexOf('\u00A4') == -1) {
// 'x' not "x" -- workaround bug in IBM JDK 1.4.1
errln("Currency format for " + locales[i] + " does not contain generic currency symbol:" + pattern);
}
// Create a DecimalFormat using the pattern we got and format a number
DecimalFormatSymbols symbols = new DecimalFormatSymbols(locales[i]);
DecimalFormat fmt1 = new DecimalFormat(pattern, symbols);
String result1 = fmt1.format(1.111);
//
// Now substitute in the locale's currency symbol and create another
// pattern. Replace the decimal separator with the monetary separator.
//
// char decSep = symbols.getDecimalSeparator(); //The variable is never used
char monSep = symbols.getMonetaryDecimalSeparator();
StringBuffer buf = new StringBuffer(pattern);
for (int j = 0; j < buf.length(); j++) {
if (buf.charAt(j) == '\u00a4') {
String cur = "'" + symbols.getCurrencySymbol() + "'";
buf.replace(j, j + 1, cur);
j += cur.length() - 1;
}
}
symbols.setDecimalSeparator(monSep);
DecimalFormat fmt2 = new DecimalFormat(buf.toString(), symbols);
// Actual width of decimal fractions and rounding option are inherited
// from the currency, not the pattern itself. So we need to force
// maximum/minimumFractionDigits and rounding option for the second
// DecimalForamt instance. The fix for ticket#7282 requires this test
// code change to make it work properly.
fmt2.setMaximumFractionDigits(fmt1.getMaximumFractionDigits());
fmt2.setMinimumFractionDigits(fmt1.getMinimumFractionDigits());
fmt2.setRoundingIncrement(fmt1.getRoundingIncrement());
String result2 = fmt2.format(1.111);
// NOTE: en_IN is a special case (ChoiceFormat currency display name)
if (!result1.equals(result2) && !locales[i].toString().equals("en_IN")) {
errln("Results for " + locales[i] + " differ: " + result1 + " vs " + result2);
}
}
}
use of android.icu.text.DecimalFormat in project j2objc by google.
the class MyNumberFormat method Test4052223.
/**
* Tests ParsePosition.setErrorPosition() and ParsePosition.getErrorPosition().
*/
@Test
public void Test4052223() {
try {
DecimalFormat fmt = new DecimalFormat("#,#00.00");
Number num = fmt.parse("abc3");
errln("Bug 4052223 failed : can't parse string \"a\". Got " + num);
} catch (ParseException foo) {
logln("Caught expected ParseException : " + foo.getMessage() + " at index : " + foo.getErrorOffset());
}
}
Aggregations