use of android.icu.math.BigDecimal in project j2objc by google.
the class TimeScaleMonkeyTest method initRandom.
private void initRandom(long min, long max) {
BigDecimal interval = new BigDecimal(max).subtract(new BigDecimal(min));
ranMin = min;
ranMax = max;
ranInt = 0;
if (ran == null) {
ran = createRandom();
}
if (interval.compareTo(longMax) < 0) {
ranInt = interval.longValue();
}
}
use of android.icu.math.BigDecimal in project j2objc by google.
the class NumberFormatTest method checkRound.
private BigDecimal checkRound(DecimalFormat nf, BigDecimal iValue, BigDecimal lastParsed) {
String formatedBigDecimal = nf.format(iValue);
String formattedDouble = nf.format(iValue.doubleValue());
if (!equalButForTrailingZeros(formatedBigDecimal, formattedDouble)) {
errln("Failure at: " + iValue + " (" + iValue.doubleValue() + ")" + ",\tRounding-mode: " + roundingModeNames[nf.getRoundingMode()] + ",\tRounding-increment: " + nf.getRoundingIncrement() + ",\tdouble: " + formattedDouble + ",\tBigDecimal: " + formatedBigDecimal);
} else {
logln("Value: " + iValue + ",\tRounding-mode: " + roundingModeNames[nf.getRoundingMode()] + ",\tRounding-increment: " + nf.getRoundingIncrement() + ",\tdouble: " + formattedDouble + ",\tBigDecimal: " + formatedBigDecimal);
}
try {
// Number should have compareTo(...)
BigDecimal parsed = toBigDecimal(nf.parse(formatedBigDecimal));
if (lastParsed.compareTo(parsed) > 0) {
errln("Rounding wrong direction!: " + lastParsed + " > " + parsed);
}
lastParsed = parsed;
} catch (ParseException e) {
errln("Parse Failure with: " + formatedBigDecimal);
}
return lastParsed;
}
use of android.icu.math.BigDecimal 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.math.BigDecimal in project j2objc by google.
the class NumberFormatTest method TestRoundUnnecessarytIssue11808.
// Testing for Issue 11808.
@Test
public void TestRoundUnnecessarytIssue11808() {
DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance();
StringBuffer result = new StringBuffer("");
df.setRoundingMode(BigDecimal.ROUND_UNNECESSARY);
df.applyPattern("00.0#E0");
try {
df.format(99999.0, result, new FieldPosition(0));
fail("Missing ArithmeticException for double: " + result);
} catch (ArithmeticException expected) {
// The exception should be thrown, since rounding is needed.
}
try {
result = df.format(99999, result, new FieldPosition(0));
fail("Missing ArithmeticException for int: " + result);
} catch (ArithmeticException expected) {
// The exception should be thrown, since rounding is needed.
}
try {
result = df.format(new BigInteger("999999"), result, new FieldPosition(0));
fail("Missing ArithmeticException for BigInteger: " + result);
} catch (ArithmeticException expected) {
// The exception should be thrown, since rounding is needed.
}
try {
result = df.format(new BigDecimal("99999"), result, new FieldPosition(0));
fail("Missing ArithmeticException for BigDecimal: " + result);
} catch (ArithmeticException expected) {
// The exception should be thrown, since rounding is needed.
}
try {
result = df.format(new BigDecimal("-99999"), result, new FieldPosition(0));
fail("Missing ArithmeticException for BigDecimal: " + result);
} catch (ArithmeticException expected) {
// The exception should be thrown, since rounding is needed.
}
}
use of android.icu.math.BigDecimal in project j2objc by google.
the class NumberFormatTest method TestCurrency.
/**
* Test localized currency patterns.
*/
@Test
public void TestCurrency() {
String[] DATA = { "fr", "CA", "", "1,50\u00a0$", "de", "DE", "", "1,50\u00a0\u20AC", "de", "DE", "PREEURO", "1,50\u00a0DM", "fr", "FR", "", "1,50\u00a0\u20AC", "fr", "FR", "PREEURO", "1,50\u00a0F" };
for (int i = 0; i < DATA.length; i += 4) {
Locale locale = new Locale(DATA[i], DATA[i + 1], DATA[i + 2]);
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
String s = fmt.format(1.50);
if (s.equals(DATA[i + 3])) {
logln("Ok: 1.50 x " + locale + " => " + s);
} else {
logln("FAIL: 1.50 x " + locale + " => " + s + ", expected " + DATA[i + 3]);
}
}
// format currency with CurrencyAmount
for (int i = 0; i < DATA.length; i += 4) {
Locale locale = new Locale(DATA[i], DATA[i + 1], DATA[i + 2]);
Currency curr = Currency.getInstance(locale);
logln("\nName of the currency is: " + curr.getName(locale, Currency.LONG_NAME, new boolean[] { false }));
CurrencyAmount cAmt = new CurrencyAmount(1.5, curr);
// cover hashCode
logln("CurrencyAmount object's hashCode is: " + cAmt.hashCode());
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
String sCurr = fmt.format(cAmt);
if (sCurr.equals(DATA[i + 3])) {
logln("Ok: 1.50 x " + locale + " => " + sCurr);
} else {
errln("FAIL: 1.50 x " + locale + " => " + sCurr + ", expected " + DATA[i + 3]);
}
}
// Cover MeasureFormat.getCurrencyFormat()
ULocale save = ULocale.getDefault();
ULocale.setDefault(ULocale.US);
MeasureFormat curFmt = MeasureFormat.getCurrencyFormat();
String strBuf = curFmt.format(new CurrencyAmount(new Float(1234.56), Currency.getInstance("USD")));
try {
CurrencyAmount parsedVal = (CurrencyAmount) curFmt.parseObject(strBuf);
Number val = parsedVal.getNumber();
if (!val.equals(new BigDecimal("1234.56"))) {
errln("FAIL: getCurrencyFormat of default locale (en_US) failed roundtripping the number. val=" + val);
}
if (!parsedVal.getCurrency().equals(Currency.getInstance("USD"))) {
errln("FAIL: getCurrencyFormat of default locale (en_US) failed roundtripping the currency");
}
} catch (ParseException e) {
errln("FAIL: " + e.getMessage());
}
ULocale.setDefault(save);
}
Aggregations