Search in sources :

Example 11 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols 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 12 with DecimalFormatSymbols

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

the class DecimalFormatSymbolsTest method test_equalsLjava_lang_Object.

/**
     * @tests java.text.DecimalFormatSymbols#equals(java.lang.Object)
     */
public void test_equalsLjava_lang_Object() {
    assertTrue("Equal objects returned false", dfs.equals(dfs.clone()));
    dfs.setDigit('B');
    assertTrue("Un-Equal objects returned true", !dfs.equals(new DecimalFormatSymbols()));
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols)

Example 13 with DecimalFormatSymbols

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

the class DecimalFormatSymbolsTest method test_getCurrency.

/**
     * @tests java.text.DecimalFormatSymbols#getCurrency()
     */
public void test_getCurrency() {
    Currency currency = Currency.getInstance("USD");
    assertEquals("Returned incorrect currency", dfsUS.getCurrency(), currency);
    Currency currK = Currency.getInstance("KRW");
    Currency currX = Currency.getInstance("XXX");
    Currency currE = Currency.getInstance("EUR");
    // Currency currF = Currency.getInstance("FRF");
    DecimalFormatSymbols dfs1 = new DecimalFormatSymbols(new Locale("ko", "KR"));
    assertTrue("Test1: Returned incorrect currency", dfs1.getCurrency() == currK);
    assertEquals("Test1: Returned incorrect currencySymbol", "₩", dfs1.getCurrencySymbol());
    assertEquals("Test1: Returned incorrect intlCurrencySymbol", "KRW", dfs1.getInternationalCurrencySymbol());
    dfs1 = new DecimalFormatSymbols(new Locale("", "KR"));
    assertTrue("Test2: Returned incorrect currency", dfs1.getCurrency() == currK);
    assertEquals("Test2: Returned incorrect currencySymbol", "₩", dfs1.getCurrencySymbol());
    assertEquals("Test2: Returned incorrect intlCurrencySymbol", "KRW", dfs1.getInternationalCurrencySymbol());
    dfs1 = new DecimalFormatSymbols(new Locale("ko", ""));
    assertTrue("Test3: Returned incorrect currency", dfs1.getCurrency() == currX);
    assertEquals("Test3: Returned incorrect currencySymbol", "¤", dfs1.getCurrencySymbol());
    assertEquals("Test3: Returned incorrect intlCurrencySymbol", "XXX", dfs1.getInternationalCurrencySymbol());
    dfs1 = new DecimalFormatSymbols(new Locale("fr", "FR"));
    assertTrue("Test4: Returned incorrect currency", dfs1.getCurrency() == currE);
    assertEquals("Test4: Returned incorrect currencySymbol", "€", dfs1.getCurrencySymbol());
    assertEquals("Test4: Returned incorrect intlCurrencySymbol", "EUR", dfs1.getInternationalCurrencySymbol());
// RI fails these tests since it doesn't have the PREEURO variant
// dfs1 = new DecimalFormatSymbols(new Locale("fr", "FR","PREEURO"));
// assertTrue("Test5: Returned incorrect currency", dfs1.getCurrency()
// == currF);
// assertTrue("Test5: Returned incorrect currencySymbol",
// dfs1.getCurrencySymbol().equals("F"));
// assertTrue("Test5: Returned incorrect intlCurrencySymbol",
// dfs1.getInternationalCurrencySymbol().equals("FRF"));
}
Also used : Locale(java.util.Locale) DecimalFormatSymbols(java.text.DecimalFormatSymbols) Currency(java.util.Currency)

Example 14 with DecimalFormatSymbols

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

the class DecimalFormatSymbolsTest method test_getInstanceLjava_util_Locale.

public void test_getInstanceLjava_util_Locale() {
    try {
        DecimalFormatSymbols.getInstance(null);
        fail();
    } catch (NullPointerException expected) {
    }
    assertEquals(new DecimalFormatSymbols(Locale.GERMANY), DecimalFormatSymbols.getInstance(Locale.GERMANY));
    Locale locale = new Locale("not exist language", "not exist country");
    DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
    assertNotNull(symbols);
}
Also used : Locale(java.util.Locale) DecimalFormatSymbols(java.text.DecimalFormatSymbols)

Example 15 with DecimalFormatSymbols

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

the class DecimalFormatSymbolsTest method testSerializationSelf.

/**
     * @tests serialization/deserialization compatibility.
     */
public void testSerializationSelf() throws Exception {
    DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.ITALIAN);
    SerializationTest.verifySelf(symbols);
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols)

Aggregations

DecimalFormatSymbols (java.text.DecimalFormatSymbols)133 DecimalFormat (java.text.DecimalFormat)93 NumberFormat (java.text.NumberFormat)14 Locale (java.util.Locale)13 ParseException (java.text.ParseException)8 BigDecimal (java.math.BigDecimal)7 Currency (java.util.Currency)7 ObjectInputStream (java.io.ObjectInputStream)6 Test (org.junit.Test)6 IOException (java.io.IOException)5 ParsePosition (java.text.ParsePosition)5 BufferedChecksumIndexInput (org.apache.lucene.store.BufferedChecksumIndexInput)5 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)5 IndexInput (org.apache.lucene.store.IndexInput)5 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 BytesRef (org.apache.lucene.util.BytesRef)4 ArrayList (java.util.ArrayList)3