use of android.icu.util.CurrencyAmount in project j2objc by google.
the class CompactDecimalFormatTest method TestBug12689.
@Test
public void TestBug12689() {
if (logKnownIssue("12689", "CDF fails for numbers less than 1 thousand in most locales")) {
return;
}
CompactDecimalFormat cdf;
String result;
cdf = CompactDecimalFormat.getInstance(ULocale.forLanguageTag("en"), CompactStyle.SHORT);
result = cdf.format(new CurrencyAmount(123, Currency.getInstance("USD")));
assertEquals("CDF should correctly format 123 with currency in 'en'", "$120", result);
cdf = CompactDecimalFormat.getInstance(ULocale.forLanguageTag("it"), CompactStyle.SHORT);
result = cdf.format(new CurrencyAmount(123, Currency.getInstance("EUR")));
assertEquals("CDF should correctly format 123 with currency in 'it'", "120 €", result);
}
use of android.icu.util.CurrencyAmount in project j2objc by google.
the class CompactDecimalFormatTest method TestBug12688.
@Test
public void TestBug12688() {
if (logKnownIssue("12688", "CDF fails for numbers less than 1 million in 'it'")) {
return;
}
CompactDecimalFormat cdf;
String result;
cdf = CompactDecimalFormat.getInstance(ULocale.forLanguageTag("it"), CompactStyle.SHORT);
result = cdf.format(new CurrencyAmount(123000, Currency.getInstance("EUR")));
assertEquals("CDF should correctly format 123000 with currency in 'it'", "120000 €", result);
}
use of android.icu.util.CurrencyAmount in project j2objc by google.
the class MeasureFormat method formatMeasure.
private StringBuilder formatMeasure(Measure measure, ImmutableNumberFormat nf, StringBuilder appendTo, FieldPosition fieldPosition) {
Number n = measure.getNumber();
MeasureUnit unit = measure.getUnit();
if (unit instanceof Currency) {
return appendTo.append(currencyFormat.format(new CurrencyAmount(n, (Currency) unit), new StringBuffer(), fieldPosition));
}
StringBuffer formattedNumber = new StringBuffer();
StandardPlural pluralForm = QuantityFormatter.selectPlural(n, nf.nf, rules, formattedNumber, fieldPosition);
String formatter = getPluralFormatter(unit, formatWidth, pluralForm.ordinal());
return QuantityFormatter.format(formatter, formattedNumber, appendTo, fieldPosition);
}
use of android.icu.util.CurrencyAmount in project j2objc by google.
the class NumberFormatTest method TestParseCurrency.
@Test
public void TestParseCurrency() {
class ParseCurrencyItem {
private final String localeString;
private final String descrip;
private final String currStr;
private final int numExpectPos;
private final int numExpectVal;
private final int curExpectPos;
private final int curExpectVal;
private final String curExpectCurr;
ParseCurrencyItem(String locStr, String desc, String curr, int numExPos, int numExVal, int curExPos, int curExVal, String curExCurr) {
localeString = locStr;
descrip = desc;
currStr = curr;
numExpectPos = numExPos;
numExpectVal = numExVal;
curExpectPos = curExPos;
curExpectVal = curExVal;
curExpectCurr = curExCurr;
}
public String getLocaleString() {
return localeString;
}
public String getDescrip() {
return descrip;
}
public String getCurrStr() {
return currStr;
}
public int getNumExpectPos() {
return numExpectPos;
}
public int getNumExpectVal() {
return numExpectVal;
}
public int getCurExpectPos() {
return curExpectPos;
}
public int getCurExpectVal() {
return curExpectVal;
}
public String getCurExpectCurr() {
return curExpectCurr;
}
}
final ParseCurrencyItem[] parseCurrencyItems = { new ParseCurrencyItem("en_US", "dollars2", "$2.00", 5, 2, 5, 2, "USD"), new ParseCurrencyItem("en_US", "dollars4", "$4", 2, 4, 2, 4, "USD"), new ParseCurrencyItem("en_US", "dollars9", "9\u00A0$", 0, 0, 0, 0, ""), new ParseCurrencyItem("en_US", "pounds3", "\u00A33.00", 0, 0, 5, 3, "GBP"), new ParseCurrencyItem("en_US", "pounds5", "\u00A35", 0, 0, 2, 5, "GBP"), new ParseCurrencyItem("en_US", "pounds7", "7\u00A0\u00A3", 0, 0, 0, 0, ""), new ParseCurrencyItem("en_US", "euros8", "\u20AC8", 0, 0, 2, 8, "EUR"), new ParseCurrencyItem("en_GB", "pounds3", "\u00A33.00", 5, 3, 5, 3, "GBP"), new ParseCurrencyItem("en_GB", "pounds5", "\u00A35", 2, 5, 2, 5, "GBP"), new ParseCurrencyItem("en_GB", "pounds7", "7\u00A0\u00A3", 0, 0, 0, 0, ""), new ParseCurrencyItem("en_GB", "euros4", "4,00\u00A0\u20AC", 0, 0, 0, 0, ""), new ParseCurrencyItem("en_GB", "euros6", "6\u00A0\u20AC", 0, 0, 0, 0, ""), new ParseCurrencyItem("en_GB", "euros8", "\u20AC8", 0, 0, 2, 8, "EUR"), new ParseCurrencyItem("en_GB", "dollars4", "US$4", 0, 0, 4, 4, "USD"), new ParseCurrencyItem("fr_FR", "euros4", "4,00\u00A0\u20AC", 6, 4, 6, 4, "EUR"), new ParseCurrencyItem("fr_FR", "euros6", "6\u00A0\u20AC", 3, 6, 3, 6, "EUR"), new ParseCurrencyItem("fr_FR", "euros8", "\u20AC8", 0, 0, 0, 0, ""), new ParseCurrencyItem("fr_FR", "dollars2", "$2.00", 0, 0, 0, 0, ""), new ParseCurrencyItem("fr_FR", "dollars4", "$4", 0, 0, 0, 0, "") };
for (ParseCurrencyItem item : parseCurrencyItems) {
String localeString = item.getLocaleString();
ULocale uloc = new ULocale(localeString);
NumberFormat fmt = null;
try {
fmt = NumberFormat.getCurrencyInstance(uloc);
} catch (Exception e) {
errln("NumberFormat.getCurrencyInstance fails for locale " + localeString);
continue;
}
String currStr = item.getCurrStr();
ParsePosition parsePos = new ParsePosition(0);
Number numVal = fmt.parse(currStr, parsePos);
if (parsePos.getIndex() != item.getNumExpectPos() || (numVal != null && numVal.intValue() != item.getNumExpectVal())) {
if (numVal != null) {
errln("NumberFormat.getCurrencyInstance parse " + localeString + "/" + item.getDescrip() + ", expect pos/val " + item.getNumExpectPos() + "/" + item.getNumExpectVal() + ", get " + parsePos.getIndex() + "/" + numVal.intValue());
} else {
errln("NumberFormat.getCurrencyInstance parse " + localeString + "/" + item.getDescrip() + ", expect pos/val " + item.getNumExpectPos() + "/" + item.getNumExpectVal() + ", get " + parsePos.getIndex() + "/(NULL)");
}
}
parsePos.setIndex(0);
CurrencyAmount currAmt = fmt.parseCurrency(currStr, parsePos);
if (parsePos.getIndex() != item.getCurExpectPos() || (currAmt != null && (currAmt.getNumber().intValue() != item.getCurExpectVal() || currAmt.getCurrency().getCurrencyCode().compareTo(item.getCurExpectCurr()) != 0))) {
if (currAmt != null) {
errln("NumberFormat.getCurrencyInstance parseCurrency " + localeString + "/" + item.getDescrip() + ", expect pos/val/curr " + item.getCurExpectPos() + "/" + item.getCurExpectVal() + "/" + item.getCurExpectCurr() + ", get " + parsePos.getIndex() + "/" + currAmt.getNumber().intValue() + "/" + currAmt.getCurrency().getCurrencyCode());
} else {
errln("NumberFormat.getCurrencyInstance parseCurrency " + localeString + "/" + item.getDescrip() + ", expect pos/val/curr " + item.getCurExpectPos() + "/" + item.getCurExpectVal() + "/" + item.getCurExpectCurr() + ", get " + parsePos.getIndex() + "/(NULL)");
}
}
}
}
use of android.icu.util.CurrencyAmount 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