android.icu.text.NumberFormat Java Examples
The following examples show how to use
android.icu.text.NumberFormat.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BigNumberFormatTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * Test the handling of the AlphaWorks BigDecimal */ @Test public void TestAlphaBigDecimal() { DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US); /*For ICU compatibility [Richard/GCL]*/ expect(NumberFormat.getScientificInstance(Locale.US), new Number[] { new android.icu.math.BigDecimal("12345.678901"), }, "1.2345678901E4"); expect(new DecimalFormat("##0.####E0", US), new Number[] { new android.icu.math.BigDecimal("12345.4999"), new android.icu.math.BigDecimal("12344.5001"), }, "12.345E3"); expect(new DecimalFormat("##0.####E0", US), new Number[] { new android.icu.math.BigDecimal("12345.5000"), new android.icu.math.BigDecimal("12346.5000"), }, "12.346E3"); }
Example #2
Source File: TestCLDRVsICU.java From j2objc with Apache License 2.0 | 6 votes |
@Ignore @Test public void TestFiles() throws SAXException, IOException { // only get ICU's locales Set s = new TreeSet(); addLocales(NumberFormat.getAvailableULocales(), s); addLocales(DateFormat.getAvailableULocales(), s); // johnvu: Collator was originally disabled // addLocales(Collator.getAvailableULocales(), s); // filter, to make tracking down bugs easier for (Iterator it = s.iterator(); it.hasNext();) { String locale = (String) it.next(); if (!LOCALE_MATCH.reset(locale).matches()) continue; _test(locale); } }
Example #3
Source File: MeasureUnitTest.java From j2objc with Apache License 2.0 | 6 votes |
@Test public void Test10219FractionalPlurals() { double[] values = {1.588, 1.011}; String[][] expected = { {"1 minute", "1.5 minutes", "1.58 minutes"}, {"1 minute", "1.0 minutes", "1.01 minutes"} }; for (int j = 0; j < values.length; j++) { for (int i = 0; i < expected[j].length; i++) { NumberFormat nf = NumberFormat.getNumberInstance(ULocale.ENGLISH); nf.setRoundingMode(BigDecimal.ROUND_DOWN); nf.setMinimumFractionDigits(i); nf.setMaximumFractionDigits(i); MeasureFormat mf = MeasureFormat.getInstance( ULocale.ENGLISH, FormatWidth.WIDE, nf); assertEquals("Test10219", expected[j][i], mf.format(new Measure(values[j], MeasureUnit.MINUTE))); } } }
Example #4
Source File: MeasureUnitTest.java From j2objc with Apache License 2.0 | 6 votes |
@Test public void testDoubleZero() { ULocale en = new ULocale("en"); NumberFormat nf = NumberFormat.getInstance(en); nf.setMinimumFractionDigits(2); nf.setMaximumFractionDigits(2); MeasureFormat mf = MeasureFormat.getInstance(en, FormatWidth.WIDE, nf); assertEquals( "Positive Rounding", "4 hours, 23 minutes, 16.00 seconds", mf.formatMeasures( new Measure(4.7, MeasureUnit.HOUR), new Measure(23, MeasureUnit.MINUTE), new Measure(16, MeasureUnit.SECOND))); assertEquals( "Negative Rounding", "-4 hours, 23 minutes, 16.00 seconds", mf.formatMeasures( new Measure(-4.7, MeasureUnit.HOUR), new Measure(23, MeasureUnit.MINUTE), new Measure(16, MeasureUnit.SECOND))); }
Example #5
Source File: NumberRegressionTests.java From j2objc with Apache License 2.0 | 6 votes |
/** * DecimalFormat formats 1.001 to "1.00" instead of "1" with 2 fraction * digits. */ @Test public void Test4217661() { Object[] DATA = { new Double(0.001), "0", new Double(1.001), "1", new Double(0.006), "0.01", new Double(1.006), "1.01", }; NumberFormat fmt = NumberFormat.getInstance(Locale.US); fmt.setMaximumFractionDigits(2); for (int i=0; i<DATA.length; i+=2) { String s = fmt.format(((Double) DATA[i]).doubleValue()); if (!s.equals(DATA[i+1])) { errln("FAIL: Got " + s + ", exp " + DATA[i+1]); } } }
Example #6
Source File: NumberRegressionTests.java From j2objc with Apache License 2.0 | 6 votes |
/** * NumberFormat cannot format Double.MAX_VALUE */ @Test public void Test4162198() { double dbl = Double.MAX_VALUE; NumberFormat f = NumberFormat.getInstance(); f.setMaximumFractionDigits(Integer.MAX_VALUE); f.setMaximumIntegerDigits(Integer.MAX_VALUE); String s = f.format(dbl); logln("The number " + dbl + " formatted to " + s); Number n = null; try { n = f.parse(s); } catch (java.text.ParseException e) { errln("Caught a ParseException:"); e.printStackTrace(); } logln("The string " + s + " parsed as " + n); if (n.doubleValue() != dbl) { errln("Round trip failure"); } }
Example #7
Source File: NumberRegressionTests.java From j2objc with Apache License 2.0 | 6 votes |
/** * DecimalFormat.parse() fails for mulipliers 2^n. */ @Test public void Test4216742() throws ParseException { DecimalFormat fmt = (DecimalFormat) NumberFormat.getInstance(Locale.US); long[] DATA = { Long.MIN_VALUE, Long.MAX_VALUE, -100000000L, 100000000L}; for (int i=0; i<DATA.length; ++i) { String str = Long.toString(DATA[i]); for (int m = 1; m <= 100; m++) { fmt.setMultiplier(m); long n = ((Number) fmt.parse(str)).longValue(); if (n > 0 != DATA[i] > 0) { errln("\"" + str + "\" parse(x " + fmt.getMultiplier() + ") => " + n); } } } }
Example #8
Source File: CalendarRegressionTest.java From j2objc with Apache License 2.0 | 6 votes |
@Test public void Test4106136() { Locale saveLocale = Locale.getDefault(); String[] names = { "Calendar", "DateFormat", "NumberFormat" }; try { Locale[] locales = { Locale.CHINESE, Locale.CHINA }; for (int i=0; i<locales.length; ++i) { Locale.setDefault(locales[i]); int[] n = { Calendar.getAvailableLocales().length, DateFormat.getAvailableLocales().length, NumberFormat.getAvailableLocales().length }; for (int j=0; j<n.length; ++j) { if (n[j] == 0) errln("Fail: " + names[j] + " has no locales for " + locales[i]); } } } finally { Locale.setDefault(saveLocale); } }
Example #9
Source File: NumberRegressionTests.java From j2objc with Apache License 2.0 | 6 votes |
/** * NumberFormat does not parse negative zero. */ @Test public void Test4162852() throws ParseException { for (int i=0; i<2; ++i) { NumberFormat f = (i == 0) ? NumberFormat.getInstance() : NumberFormat.getPercentInstance(); double d = -0.0; String s = f.format(d); double e = f.parse(s).doubleValue(); logln("" + d + " -> " + '"' + s + '"' + " -> " + e); if (e != 0.0 || 1.0/e > 0.0) { logln("Failed to parse negative zero"); } } }
Example #10
Source File: MessageRegressionTest.java From j2objc with Apache License 2.0 | 6 votes |
@Test public void TestSerialization() { MessageFormat format1 = null; MessageFormat format2 = null; format1 = new MessageFormat("", ULocale.GERMAN); format2 = serializeAndDeserialize(format1); assertEquals("MessageFormats (empty pattern) before and after serialization are not equal", format1, format2); format1.applyPattern("ab{1}cd{0,number}ef{3,date}gh"); format1.setFormat(2, null); format1.setFormatByArgumentIndex(1, NumberFormat.getInstance(ULocale.ENGLISH)); format2 = serializeAndDeserialize(format1); assertEquals("MessageFormats (with custom formats) before and after serialization are not equal", format1, format2); assertEquals( "MessageFormat (with custom formats) does not "+ "format correctly after serialization", "ab3.3cd4,4ef***gh", format2.format(new Object[] { 4.4, 3.3, "+++", "***" })); }
Example #11
Source File: RelativeDateTimeFormatterTest.java From j2objc with Apache License 2.0 | 6 votes |
@Test public void TestJavaLocale() { Locale loc = Locale.US; double amount = 12.3456d; RelativeDateTimeFormatter fmt = RelativeDateTimeFormatter.getInstance(loc); String s = fmt.format(amount, Direction.LAST, RelativeUnit.SECONDS); assertEquals("Java Locale.US", "12.346 seconds ago", s); // Modified instance NumberFormat nf = fmt.getNumberFormat(); nf.setMaximumFractionDigits(1); fmt = RelativeDateTimeFormatter.getInstance(loc, nf); s = fmt.format(amount, Direction.LAST, RelativeUnit.SECONDS); assertEquals("Java Locale.US", "12.3 seconds ago", s); }
Example #12
Source File: TimeUnitTest.java From j2objc with Apache License 2.0 | 6 votes |
@Test public void TestAPI() { TimeUnitFormat format = new TimeUnitFormat(); format.setLocale(new ULocale("pt_BR")); formatParsing(format); format = new TimeUnitFormat(new ULocale("de")); formatParsing(format); format = new TimeUnitFormat(new ULocale("ja")); format.setNumberFormat(NumberFormat.getNumberInstance(new ULocale("en"))); formatParsing(format); format = new TimeUnitFormat(); ULocale es = new ULocale("es"); format.setNumberFormat(NumberFormat.getNumberInstance(es)); format.setLocale(es); formatParsing(format); format.setLocale(new Locale("pt_BR")); formatParsing(format); format = new TimeUnitFormat(new Locale("de")); formatParsing(format); format = new TimeUnitFormat(new Locale("ja")); format.setNumberFormat(NumberFormat.getNumberInstance(new Locale("en"))); formatParsing(format); }
Example #13
Source File: BigNumberFormatTest.java From j2objc with Apache License 2.0 | 6 votes |
private void expect(NumberFormat fmt, String str, Number exp) { Number saw = null; try { saw = fmt.parse(str); } catch (ParseException e) { saw = null; } String pat = ((DecimalFormat) fmt).toPattern(); if (saw.equals(exp)) { logln("Ok \"" + str + "\" x " + pat + " = " + showNumber(saw)); } else { errln("FAIL \"" + str + "\" x " + pat + " = " + showNumber(saw) + ", expected " + showNumber(exp)); } }
Example #14
Source File: IntlTestDecimalFormatAPI.java From j2objc with Apache License 2.0 | 6 votes |
@Test public void testJB6134() { DecimalFormat decfmt = new DecimalFormat(); StringBuffer buf = new StringBuffer(); FieldPosition fposByInt = new FieldPosition(NumberFormat.INTEGER_FIELD); decfmt.format(123, buf, fposByInt); buf.setLength(0); FieldPosition fposByField = new FieldPosition(NumberFormat.Field.INTEGER); decfmt.format(123, buf, fposByField); if (fposByInt.getEndIndex() != fposByField.getEndIndex()) { errln("ERROR: End index for integer field - fposByInt:" + fposByInt.getEndIndex() + " / fposByField: " + fposByField.getEndIndex()); } }
Example #15
Source File: NumberFormatRegressionTest.java From j2objc with Apache License 2.0 | 6 votes |
void checkNBSPPatternRtNum(String testcase, NumberFormat nf, double myNumber) { String myString = nf.format(myNumber); double aNumber; try { aNumber = nf.parse(myString).doubleValue(); } catch (ParseException e) { // TODO Auto-generated catch block errln("FAIL: " + testcase +" - failed to parse. " + e.toString()); return; } if(Math.abs(aNumber-myNumber)>.001) { errln("FAIL: "+testcase+": formatted "+myNumber+", parsed into "+aNumber+"\n"); } else { logln("PASS: "+testcase+": formatted "+myNumber+", parsed into "+aNumber+"\n"); } }
Example #16
Source File: PluralFormatUnitTest.java From j2objc with Apache License 2.0 | 6 votes |
@Test public void TestSetLocale() { // Create rules for testing. PluralRules oddAndEven = PluralRules.createRules("odd__: n mod 2 is 1"); PluralFormat plFmt = new PluralFormat(oddAndEven); plFmt.applyPattern("odd__{odd} other{even}"); plFmt.setLocale(ULocale.ENGLISH); // Check that pattern gets deleted. NumberFormat nrFmt = NumberFormat.getInstance(ULocale.ENGLISH); assertEquals("pattern was not resetted by setLocale() call.", nrFmt.format(5), plFmt.format(5)); // Check that rules got updated. plFmt.applyPattern("odd__{odd} other{even}"); assertEquals("SetLocale should reset rules but did not.", "even", plFmt.format(1)); plFmt.applyPattern("one{one} other{not one}"); for (int i = 0; i < 20; ++i) { assertEquals("Wrong ruleset loaded by setLocale()", ((i==1) ? "one" : "not one"), plFmt.format(i)); } }
Example #17
Source File: WriteNumberFormatSerialTestData.java From j2objc with Apache License 2.0 | 6 votes |
public static void main(String[] args){ NumberFormat nf = NumberFormat.getInstance(Locale.US); NumberFormat nfc = NumberFormat.getCurrencyInstance(Locale.US); NumberFormat nfp = NumberFormat.getPercentInstance(Locale.US); NumberFormat nfsp = NumberFormat.getScientificInstance(Locale.US); try{ FileOutputStream file = new FileOutputStream("NumberFormatSerialTestData.java"); file.write(header.getBytes()); write(file,(Object)nf,"generalInstance", "//NumberFormat.getInstance(Locale.US)"); write(file,(Object)nfc,"currencyInstance","//NumberFormat.getCurrencyInstance(Locale.US)"); write(file,(Object)nfp,"percentInstance","//NumberFormat.getPercentInstance(Locale.US)"); write(file,(Object)nfsp,"scientificInstance","//NumberFormat.getScientificInstance(Locale.US)"); file.write(footer.getBytes()); file.close(); }catch( Exception e){ System.out.println(e.getMessage()); e.printStackTrace(); } }
Example #18
Source File: NumberFormatRegressionTest.java From j2objc with Apache License 2.0 | 6 votes |
@Test public void TestT9293() { NumberFormat fmt = NumberFormat.getCurrencyInstance(); fmt.setParseStrict(true); final int val = 123456; String txt = fmt.format(123456); ParsePosition pos = new ParsePosition(0); Number num = fmt.parse(txt, pos); if (pos.getErrorIndex() >= 0) { errln("FAIL: Parsing " + txt + " - error index: " + pos.getErrorIndex()); } else if (val != num.intValue()) { errln("FAIL: Parsed result: " + num + " - expected: " + val); } }
Example #19
Source File: IntlTestNumberFormat.java From j2objc with Apache License 2.0 | 6 votes |
/** * Internal use */ private void _testLocale(Locale locale) { String localeName = locale + " (" + locale.getDisplayName() + ")"; logln("Number test " + localeName); fNumberFormat = NumberFormat.getInstance(locale); _testFormat(); logln("Currency test " + localeName); fNumberFormat = NumberFormat.getCurrencyInstance(locale); _testFormat(); logln("Percent test " + localeName); fNumberFormat = NumberFormat.getPercentInstance(locale); _testFormat(); if (locale.toString().compareTo("en_US_POSIX") != 0 ) { logln("Scientific test " + localeName); fNumberFormat = NumberFormat.getScientificInstance(locale); _testFormat(); } }
Example #20
Source File: IntlTestNumberFormat.java From j2objc with Apache License 2.0 | 6 votes |
/** * call _testFormat for currency, percent and plain number instances */ @Test public void TestLocale() { Locale locale = Locale.getDefault(); String localeName = locale + " (" + locale.getDisplayName() + ")"; logln("Number test " + localeName); fNumberFormat = NumberFormat.getInstance(locale); _testFormat(); logln("Currency test " + localeName); fNumberFormat = NumberFormat.getCurrencyInstance(locale); _testFormat(); logln("Percent test " + localeName); fNumberFormat = NumberFormat.getPercentInstance(locale); _testFormat(); }
Example #21
Source File: IntlTestNumberFormat.java From j2objc with Apache License 2.0 | 6 votes |
/** * test NumberFormat::getAvailableLocales **/ @Test public void TestAvailableLocales() { final Locale[] locales = NumberFormat.getAvailableLocales(); int count = locales.length; logln(count + " available locales"); if (count != 0) { String all = ""; for (int i = 0; i< count; ++i) { if (i!=0) all += ", "; all += locales[i].getDisplayName(); } logln(all); } else errln("**** FAIL: Zero available locales or null array pointer"); }
Example #22
Source File: GlobalizationPreferences.java From j2objc with Apache License 2.0 | 5 votes |
/** * This function can be overridden by subclasses to use different heuristics. * <b>It MUST return a 'safe' value, * one whose modification will not affect this object.</b> * * @param style * @hide draft / provisional / internal are hidden on Android */ protected NumberFormat guessNumberFormat(int style) { NumberFormat result; ULocale nfLocale = getAvailableLocale(TYPE_NUMBERFORMAT); if (nfLocale == null) { nfLocale = ULocale.ROOT; } switch (style) { case NF_NUMBER: result = NumberFormat.getInstance(nfLocale); break; case NF_SCIENTIFIC: result = NumberFormat.getScientificInstance(nfLocale); break; case NF_INTEGER: result = NumberFormat.getIntegerInstance(nfLocale); break; case NF_PERCENT: result = NumberFormat.getPercentInstance(nfLocale); break; case NF_CURRENCY: result = NumberFormat.getCurrencyInstance(nfLocale); result.setCurrency(getCurrency()); break; default: throw new IllegalArgumentException("Unknown number format style"); } return result; }
Example #23
Source File: IntlTestDecimalFormatAPIC.java From j2objc with Apache License 2.0 | 5 votes |
private static List<FieldContainer> getNegativeExponentVector() { List<FieldContainer> v = new ArrayList<FieldContainer>(6); v.add(new FieldContainer(0, 4, NumberFormat.Field.INTEGER)); v.add(new FieldContainer(4, 5, NumberFormat.Field.DECIMAL_SEPARATOR)); v.add(new FieldContainer(5, 6, NumberFormat.Field.FRACTION)); v.add(new FieldContainer(6, 7, NumberFormat.Field.EXPONENT_SYMBOL)); v.add(new FieldContainer(7, 8, NumberFormat.Field.EXPONENT_SIGN)); v.add(new FieldContainer(8, 9, NumberFormat.Field.EXPONENT)); return v; }
Example #24
Source File: CompactDecimalFormatTest.java From j2objc with Apache License 2.0 | 5 votes |
@Test public void TestCharacterIterator() { CompactDecimalFormat cdf = getCDFInstance(ULocale.forLanguageTag("sw"), CompactStyle.SHORT); AttributedCharacterIterator iter = cdf.formatToCharacterIterator(1234567); assertEquals("CharacterIterator", "M1.2", iterToString(iter)); iter = cdf.formatToCharacterIterator(1234567); iter.setIndex(1); assertEquals("Attributes", NumberFormat.Field.INTEGER, iter.getAttribute(NumberFormat.Field.INTEGER)); assertEquals("Attributes", 1, iter.getRunStart()); assertEquals("Attributes", 2, iter.getRunLimit()); }
Example #25
Source File: RbnfTest.java From j2objc with Apache License 2.0 | 5 votes |
void doTest(RuleBasedNumberFormat formatter, String[][] testData, boolean testParsing) { // NumberFormat decFmt = NumberFormat.getInstance(Locale.US); NumberFormat decFmt = new DecimalFormat("#,###.################"); try { for (int i = 0; i < testData.length; i++) { String number = testData[i][0]; String expectedWords = testData[i][1]; if (isVerbose()) { logln("test[" + i + "] number: " + number + " target: " + expectedWords); } Number num = decFmt.parse(number); String actualWords = formatter.format(num); if (!actualWords.equals(expectedWords)) { errln("Spot check format failed: for " + number + ", expected\n " + expectedWords + ", but got\n " + actualWords); } else if (testParsing) { String actualNumber = decFmt.format(formatter .parse(actualWords)); if (!actualNumber.equals(number)) { errln("Spot check parse failed: for " + actualWords + ", expected " + number + ", but got " + actualNumber); } } } } catch (Throwable e) { e.printStackTrace(); errln("Test failed with exception: " + e.toString()); } }
Example #26
Source File: TestMessageFormat.java From j2objc with Apache License 2.0 | 5 votes |
public void TestDateFormatHashCode() { DateFormat testDF = DateFormat.getDateInstance(DateFormat.DEFAULT, ULocale.GERMAN); NumberFormat testNF = testDF.getNumberFormat(); int expectedResult = testNF.getMaximumIntegerDigits() * 37 + testNF.getMaximumFractionDigits(); int actualHashResult = testDF.hashCode(); assertEquals("DateFormat hashCode", expectedResult, actualHashResult); }
Example #27
Source File: IntlTestDecimalFormatAPIC.java From j2objc with Apache License 2.0 | 5 votes |
private static List<FieldContainer> getPositiveExponentVector() { List<FieldContainer> v = new ArrayList<FieldContainer>(5); v.add(new FieldContainer(0, 2, NumberFormat.Field.INTEGER)); v.add(new FieldContainer(2, 3, NumberFormat.Field.DECIMAL_SEPARATOR)); v.add(new FieldContainer(3, 5, NumberFormat.Field.FRACTION)); v.add(new FieldContainer(5, 6, NumberFormat.Field.EXPONENT_SYMBOL)); v.add(new FieldContainer(6, 7, NumberFormat.Field.EXPONENT)); return v; }
Example #28
Source File: NumberRegressionTests.java From j2objc with Apache License 2.0 | 5 votes |
/** * NumberFormat.parse doesn't return null */ @Test public void Test4114639() { NumberFormat format = NumberFormat.getInstance(); String text = "time 10:x"; ParsePosition pos = new ParsePosition(8); Number result = format.parse(text, pos); if (result != null) errln("Should return null but got : " + result); // Should be null; it isn't }
Example #29
Source File: ScientificNumberFormatterTest.java From j2objc with Apache License 2.0 | 5 votes |
@Test public void TestFixedDecimalSuperscript() { DecimalFormat decfmt = (DecimalFormat) NumberFormat.getInstance(ULocale.ENGLISH); ScientificNumberFormatter fmt = ScientificNumberFormatter.getSuperscriptInstance(decfmt); assertEquals( "", "123,456", fmt.format(123456.0)); }
Example #30
Source File: ScientificNumberFormatterTest.java From j2objc with Apache License 2.0 | 5 votes |
@Test public void TestPlusSignInExponentMarkup() { DecimalFormat decfmt = (DecimalFormat) NumberFormat.getScientificInstance(ULocale.ENGLISH); decfmt.applyPattern("0.00E+0"); ScientificNumberFormatter fmt = ScientificNumberFormatter.getMarkupInstance( decfmt, "<sup>", "</sup>"); assertEquals( "", "6.02×10<sup>+23</sup>", fmt.format(6.02e23)); }