Search in sources :

Example 46 with DecimalFormat

use of java.text.DecimalFormat in project j2objc by google.

the class DecimalFormatTest method assertDecimalFormatIsLossless.

private static void assertDecimalFormatIsLossless(double d) throws Exception {
    final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
    DecimalFormat format = new DecimalFormat("#0.#", dfs);
    format.setGroupingUsed(false);
    format.setMaximumIntegerDigits(400);
    format.setMaximumFractionDigits(400);
    // Every floating point binary can be represented exactly in decimal if you have enough
    // digits. This shows the value actually being tested.
    String testId = "decimalValue: " + new BigDecimal(d);
    // As a sanity check we try out parseDouble() with the string generated by
    // Double.toString(). Strictly speaking Double.toString() is probably not guaranteed to be
    // lossless, but in reality it probably is, or at least is close enough.
    assertDoubleEqual(testId + " failed parseDouble(toString()) sanity check", d, Double.parseDouble(Double.toString(d)));
    // Format the number: If this is lossy it is a problem. We are trying to check that it
    // doesn't lose any unnecessary precision.
    String result = format.format(d);
    // Here we use Double.parseDouble() which should able to parse a number we know was
    // representable as a double into the original double. If parseDouble() is not implemented
    // correctly the test is invalid.
    double doubleParsed = Double.parseDouble(result);
    assertDoubleEqual(testId + " (format() produced " + result + ")", d, doubleParsed);
    // For completeness we try to parse using the formatter too. If this fails but the format
    // above didn't it may be a problem with parse(), or with format() that we didn't spot.
    assertDoubleEqual(testId + " failed parse(format()) check", d, format.parse(result).doubleValue());
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) BigDecimal(java.math.BigDecimal)

Example 47 with DecimalFormat

use of java.text.DecimalFormat in project j2objc by google.

the class DecimalFormatTest method testMinimumFractionDigits_getAndSet.

public void testMinimumFractionDigits_getAndSet() {
    DecimalFormat form = (DecimalFormat) NumberFormat.getInstance(Locale.US);
    // getMinimumFractionDigits from NumberFormat (default to 0)
    // getMinimumFractionDigits from DecimalFormat (default to 0)
    assertEquals(0, form.getMinimumFractionDigits());
    form.setMinimumFractionDigits(310);
    assertEquals(310, form.getMinimumFractionDigits());
    // Deliberately > 340. The API docs mention 340 and suggest that you can set the value
    // higher but it will use 340 as a ceiling.
    form.setMinimumFractionDigits(500);
    assertEquals(500, form.getMinimumFractionDigits());
    form.setMaximumFractionDigits(400);
    assertEquals(400, form.getMinimumFractionDigits());
    form.setMinimumFractionDigits(-3);
    assertEquals(0, form.getMinimumFractionDigits());
}
Also used : DecimalFormat(java.text.DecimalFormat)

Example 48 with DecimalFormat

use of java.text.DecimalFormat in project j2objc by google.

the class DecimalFormatTest method test_hashCode.

public void test_hashCode() {
    DecimalFormat df1 = new DecimalFormat();
    DecimalFormat df2 = (DecimalFormat) df1.clone();
    assertTrue("Hash codes of equals object are not equal", df2.hashCode() == df1.hashCode());
}
Also used : DecimalFormat(java.text.DecimalFormat)

Example 49 with DecimalFormat

use of java.text.DecimalFormat in project j2objc by google.

the class DecimalFormatTest method test_setGroupingUsed.

public void test_setGroupingUsed() {
    DecimalFormat format = new DecimalFormat();
    StringBuffer buf = new StringBuffer();
    format.setGroupingUsed(false);
    format.format(new Long(1970), buf, new FieldPosition(0));
    assertEquals("1970", buf.toString());
    assertFalse(format.isGroupingUsed());
    format.format(new Long(1970), buf, new FieldPosition(0));
    assertEquals("19701970", buf.toString());
    assertFalse(format.isGroupingUsed());
    format.setGroupingUsed(true);
    format.format(new Long(1970), buf, new FieldPosition(0));
    assertEquals("197019701,970", buf.toString());
    assertTrue(format.isGroupingUsed());
}
Also used : DecimalFormat(java.text.DecimalFormat) FieldPosition(java.text.FieldPosition)

Example 50 with DecimalFormat

use of java.text.DecimalFormat in project j2objc by google.

the class DecimalFormatTest method test_formatToCharacterIterator_veryLarge.

public void test_formatToCharacterIterator_veryLarge() throws Exception {
    AttributedCharacterIterator iterator;
    int[] runStarts;
    int[] runLimits;
    String result;
    char current;
    Number number = new BigDecimal("1.23456789E1234");
    assertEquals("1.23456789E+1234", number.toString());
    iterator = new DecimalFormat().formatToCharacterIterator(number);
    runStarts = new int[] { 0, 0, 2, 3, 3, 3, 6, 7, 7, 7, 10, 11, 11, 11, 14 };
    runLimits = new int[] { 2, 2, 3, 6, 6, 6, 7, 10, 10, 10, 11, 14, 14, 14, 15 };
    // 000,000,000,000....
    result = "12,345,678,900,";
    current = iterator.current();
    for (int i = 0; i < runStarts.length; i++) {
        assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart());
        assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit());
        assertEquals("wrong char @" + i, result.charAt(i), current);
        current = iterator.next();
    }
    assertEquals(0, iterator.getBeginIndex());
    assertEquals(1646, iterator.getEndIndex());
}
Also used : DecimalFormat(java.text.DecimalFormat) BigDecimal(java.math.BigDecimal) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Aggregations

DecimalFormat (java.text.DecimalFormat)823 DecimalFormatSymbols (java.text.DecimalFormatSymbols)94 NumberFormat (java.text.NumberFormat)93 IOException (java.io.IOException)48 BigDecimal (java.math.BigDecimal)47 ArrayList (java.util.ArrayList)44 IFormatterTextCallBack (org.xclcharts.common.IFormatterTextCallBack)33 ParseException (java.text.ParseException)31 Test (org.junit.Test)31 File (java.io.File)29 IFormatterDoubleCallBack (org.xclcharts.common.IFormatterDoubleCallBack)29 Support_DecimalFormat (tests.support.Support_DecimalFormat)28 Date (java.util.Date)25 SimpleDateFormat (java.text.SimpleDateFormat)24 HashMap (java.util.HashMap)24 Locale (java.util.Locale)22 PrintWriter (java.io.PrintWriter)20 UsageVO (com.cloud.usage.UsageVO)19 List (java.util.List)18 JFreeChart (org.jfree.chart.JFreeChart)18