Java Code Examples for android.icu.text.NumberFormat#getPercentInstance()
The following examples show how to use
android.icu.text.NumberFormat#getPercentInstance() .
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: 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 2
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 3
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 4
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 5
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 6
Source File: NumberRegressionTests.java From j2objc with Apache License 2.0 | 5 votes |
/** * Tests the setMaximumFractionDigits limit. */ @Test public void Test4098741() { try { NumberFormat fmt = NumberFormat.getPercentInstance(); fmt.setMaximumFractionDigits(20); logln(fmt.format(.001)); } catch (Exception foo) { warnln("Bug 4098471 failed with exception thrown : " + foo.getMessage()); } }
Example 7
Source File: NumberFormatRoundTripTest.java From j2objc with Apache License 2.0 | 4 votes |
@Test public void TestNumberFormatRoundTrip() { NumberFormat fmt = null; logln("Default Locale"); logln("Default Number format"); fmt = NumberFormat.getInstance(); _test(fmt); logln("Currency Format"); fmt = NumberFormat.getCurrencyInstance(); _test(fmt); logln("Percent Format"); fmt = NumberFormat.getPercentInstance(); _test(fmt); int locCount = 0; final Locale[] loc = NumberFormat.getAvailableLocales(); if(quick) { if(locCount > 5) locCount = 5; logln("Quick mode: only _testing first 5 Locales"); } for(int i = 0; i < locCount; ++i) { logln(loc[i].getDisplayName()); fmt = NumberFormat.getInstance(loc[i]); _test(fmt); fmt = NumberFormat.getCurrencyInstance(loc[i]); _test(fmt); fmt = NumberFormat.getPercentInstance(loc[i]); _test(fmt); } logln("Numeric error " + min_numeric_error + " to " + max_numeric_error); }
Example 8
Source File: NumberRegressionTests.java From j2objc with Apache License 2.0 | 4 votes |
/** * Number format data rounding errors for locale FR */ @Test public void Test4070798 () { NumberFormat formatter; String tempString; /* User error : String expectedDefault = "-5\u00a0789,987"; String expectedCurrency = "5\u00a0789,98\u00a0F"; String expectedPercent = "-578\u00a0998%"; */ String expectedDefault = "-5\u00a0789,988"; String expectedCurrency = "5\u00a0789,99\u00a0" + EURO; // euro String expectedPercent = "-578\u00a0999\u00a0%"; formatter = NumberFormat.getNumberInstance(Locale.FRANCE); tempString = formatter.format (-5789.9876); if (tempString.equals(expectedDefault)) { logln ("Bug 4070798 default test passed."); } else { errln("Failed:" + " Expected " + expectedDefault + " Received " + tempString ); } formatter = NumberFormat.getCurrencyInstance(Locale.FRANCE); tempString = formatter.format( 5789.9876 ); if (tempString.equals(expectedCurrency) ) { logln ("Bug 4070798 currency test assed."); } else { errln("Failed:" + " Expected " + expectedCurrency + " Received " + tempString ); } formatter = NumberFormat.getPercentInstance(Locale.FRANCE); tempString = formatter.format (-5789.9876); if (tempString.equals(expectedPercent) ) { logln ("Bug 4070798 percentage test passed."); } else { errln("Failed:" + " Expected " + expectedPercent + " Received " + tempString ); } }
Example 9
Source File: NumberRegressionTests.java From j2objc with Apache License 2.0 | 4 votes |
/** * 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 ); } }
Example 10
Source File: NumberRegressionTests.java From j2objc with Apache License 2.0 | 4 votes |
/** * Data rounding errors for German (Germany) locale */ @Test public void Test4071014 () { NumberFormat formatter; String tempString; /* user error : String expectedDefault = "-5.789,987"; String expectedCurrency = "5.789,98\u00a0DM"; String expectedPercent = "-578.998%"; */ String expectedDefault = "-5.789,988"; String expectedCurrency = "5.789,99\u00a0" + EURO; String expectedPercent = "-578.999\u00a0%"; formatter = NumberFormat.getNumberInstance(Locale.GERMANY); tempString = formatter.format (-5789.9876); if (tempString.equals(expectedDefault)) { logln ("Bug 4071014 default test passed."); } else { errln("Failed:" + " Expected " + expectedDefault + " Received " + tempString ); } formatter = NumberFormat.getCurrencyInstance(Locale.GERMANY); tempString = formatter.format( 5789.9876 ) ; if (tempString.equals(expectedCurrency) ) { logln ("Bug 4071014 currency test passed."); } else { errln("Failed:" + " Expected " + expectedCurrency + " Received " + tempString ); } formatter = NumberFormat.getPercentInstance(Locale.GERMANY); tempString = formatter.format (-5789.9876); if (tempString.equals(expectedPercent) ) { logln ("Bug 4071014 percentage test passed."); } else { errln("Failed:" + " Expected " + expectedPercent + " Received " + tempString ); } }
Example 11
Source File: NumberRegressionTests.java From j2objc with Apache License 2.0 | 4 votes |
/** * Data rounding errors for Italian locale number formats * Note- with the Euro, there is no need for currency rounding anymore */ @Test public void Test4071859 () { NumberFormat formatter; String tempString; /* user error : String expectedDefault = "-5.789,987"; String expectedCurrency = "-L.\u00a05.789,98"; String expectedPercent = "-578.998%"; */ String expectedDefault = "-5.789,988"; String expectedCurrency = "-5.789,99\u00A0" + EURO; String expectedPercent = "-578.999%"; formatter = NumberFormat.getNumberInstance(Locale.ITALY); tempString = formatter.format (-5789.9876); if (tempString.equals(expectedDefault)) { logln ("Bug 4071859 default test passed."); } else { errln("a) Failed:" + " Expected " + expectedDefault + " Received " + tempString ); } formatter = NumberFormat.getCurrencyInstance(Locale.ITALY); tempString = formatter.format( -5789.9876 ) ; if (tempString.equals(expectedCurrency) ) { logln ("Bug 4071859 currency test passed."); } else { errln("b) Failed:" + " Expected " + expectedCurrency + " Received " + tempString ); } formatter = NumberFormat.getPercentInstance(Locale.ITALY); tempString = formatter.format (-5789.9876); if (tempString.equals(expectedPercent) ) { logln ("Bug 4071859 percentage test passed."); } else { errln("c) Failed:" + " Expected " + expectedPercent + " Received " + tempString ); } }
Example 12
Source File: TestCLDRVsICU.java From j2objc with Apache License 2.0 | 4 votes |
public void handleResult(ULocale locale, String result) { NumberFormat nf = null; double v = Double.NaN; for (Iterator it = settings.keySet().iterator(); it.hasNext();) { String attributeName = (String) it.next(); String attributeValue = (String) settings.get(attributeName); // Checks if the attribute name is a draft and whether // or not it has been approved / contributed by CLDR yet // otherwise, skips it because it is most likely rejected by ICU if (attributeName.equals("draft")) { if (attributeValue.indexOf("approved") == -1 && attributeValue.indexOf("contributed") == -1) { break; } continue; } // Update the value to be checked if (attributeName.equals("input")) { v = Double.parseDouble(attributeValue); continue; } // At this point, it must be a numberType int index = lookupValue(attributeValue, NumberNames); if (DEBUG) logln("Getting number format for " + locale); switch (index) { case 0: nf = NumberFormat.getInstance(locale); break; case 1: nf = NumberFormat.getIntegerInstance(locale); break; case 2: nf = NumberFormat.getNumberInstance(locale); break; case 3: nf = NumberFormat.getPercentInstance(locale); break; case 4: nf = NumberFormat.getScientificInstance(locale); break; default: nf = NumberFormat.getCurrencyInstance(locale); nf.setCurrency(Currency.getInstance(attributeValue)); break; } String temp = nf.format(v).trim(); result = result.trim(); // HACK because of SAX if (!temp.equals(result)) { logln("Number: Locale: " + locale + "\n\tType: " + attributeValue + "\n\tDraft: " + settings.get("draft") + "\n\tCLDR: <" + result + ">" + "\n\tICU: <" + temp + ">"); } } }