Java Code Examples for android.icu.text.NumberFormat#setMaximumFractionDigits()
The following examples show how to use
android.icu.text.NumberFormat#setMaximumFractionDigits() .
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: 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 2
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 3
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 4
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 5
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 6
Source File: BigNumberFormatTest.java From j2objc with Apache License 2.0 | 5 votes |
@Test public void Test4161100() { NumberFormat f = NumberFormat.getInstance(); f.setMinimumFractionDigits(1); f.setMaximumFractionDigits(1); double a = -0.09; String s = f.format(a); logln(a + " x " + ((DecimalFormat) f).toPattern() + " = " + s); if (!s.equals("-0.1")) { errln("FAIL"); } }
Example 7
Source File: NumberFormatRegressionTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * alphaWorks upgrade */ @Test public void Test4161100() { NumberFormat nf = NumberFormat.getInstance(Locale.US); nf.setMinimumFractionDigits(1); nf.setMaximumFractionDigits(1); double a = -0.09; String s = nf.format(a); logln(a + " x " + ((DecimalFormat) nf).toPattern() + " = " + s); if (!s.equals("-0.1")) { errln("FAIL"); } }
Example 8
Source File: TimeUnitTest.java From j2objc with Apache License 2.0 | 5 votes |
@Test public void Test10219FractionalPlurals() { TimeUnitFormat tuf = new TimeUnitFormat(ULocale.ENGLISH, TimeUnitFormat.FULL_NAME); String[] expected = {"1 minute", "1.5 minutes", "1.58 minutes"}; for (int i = 2; i >= 0; i--) { NumberFormat nf = NumberFormat.getNumberInstance(ULocale.ENGLISH); nf.setRoundingMode(BigDecimal.ROUND_DOWN); nf.setMaximumFractionDigits(i); tuf.setNumberFormat(nf); assertEquals("Test10219", expected[i], tuf.format(new TimeUnitAmount(1.588, TimeUnit.MINUTE))); } }
Example 9
Source File: NumberRegressionTests.java From j2objc with Apache License 2.0 | 5 votes |
/** * DecimalFormat does not round up correctly. */ @Test public void Test4071492 (){ double x = 0.00159999; NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(4); String out = nf.format(x); logln("0.00159999 formats with 4 fractional digits to " + out); String expected = "0.0016"; if (!out.equals(expected)) errln("FAIL: Expected " + expected); }
Example 10
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 11
Source File: RelativeDateTimeFormatterTest.java From j2objc with Apache License 2.0 | 5 votes |
@Test public void TestCustomNumberFormat() { ULocale loc = new ULocale("en_US"); NumberFormat nf = NumberFormat.getInstance(loc); nf.setMinimumFractionDigits(1); nf.setMaximumFractionDigits(1); RelativeDateTimeFormatter fmt = RelativeDateTimeFormatter.getInstance(loc, nf); // Change nf after the fact to prove that we made a defensive copy nf.setMinimumFractionDigits(3); nf.setMaximumFractionDigits(3); // Change getNumberFormat to prove we made defensive copy going out. fmt.getNumberFormat().setMinimumFractionDigits(5); assertEquals( "TestCustomNumberformat", 1, fmt.getNumberFormat().getMinimumFractionDigits()); Object[][] data = { {0.0, Direction.NEXT, RelativeUnit.SECONDS, "in 0.0 seconds"}, {0.5, Direction.NEXT, RelativeUnit.SECONDS, "in 0.5 seconds"}, {1.0, Direction.NEXT, RelativeUnit.SECONDS, "in 1.0 seconds"}, {2.0, Direction.NEXT, RelativeUnit.SECONDS, "in 2.0 seconds"}, }; for (Object[] row : data) { String actual = fmt.format( ((Double) row[0]).doubleValue(), (Direction) row[1], (RelativeUnit) row[2]); assertEquals("Relative date with quantity special NumberFormat", row[3], actual); } }
Example 12
Source File: NumberFormatRoundTripTest.java From j2objc with Apache License 2.0 | 4 votes |
private void _test(NumberFormat fmt, Number value) { logln("test data = " + value); fmt.setMaximumFractionDigits(999); String s, s2; if (value.getClass().getName().equalsIgnoreCase("java.lang.Double")) s = fmt.format(value.doubleValue()); else s = fmt.format(value.longValue()); Number n = new Double(0); boolean show = verbose; if (DEBUG) logln( /*value.getString(temp) +*/ " F> " + s); try { n = fmt.parse(s); } catch (java.text.ParseException e) { System.out.println(e); } if (DEBUG) logln(s + " P> " /*+ n.getString(temp)*/); if (value.getClass().getName().equalsIgnoreCase("java.lang.Double")) s2 = fmt.format(n.doubleValue()); else s2 = fmt.format(n.longValue()); if (DEBUG) logln(/*n.getString(temp) +*/ " F> " + s2); if (STRING_COMPARE) { if (!s.equals(s2)) { errln("*** STRING ERROR \"" + s + "\" != \"" + s2 + "\""); show = true; } } if (EXACT_NUMERIC_COMPARE) { if (value != n) { errln("*** NUMERIC ERROR"); show = true; } } else { // Compute proportional error double error = proportionalError(value, n); if (error > MAX_ERROR) { errln("*** NUMERIC ERROR " + error); show = true; } if (error > max_numeric_error) max_numeric_error = error; if (error < min_numeric_error) min_numeric_error = error; } if (show) logln( /*value.getString(temp) +*/ value.getClass().getName() + " F> " + s + " P> " + /*n.getString(temp) +*/ n.getClass().getName() + " F> " + s2); }