Search in sources :

Example 46 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project robovm by robovm.

the class DecimalFormatSymbolsTest method testSerialization.

// http://code.google.com/p/android/issues/detail?id=14495
public void testSerialization() throws Exception {
    DecimalFormatSymbols originalDfs = DecimalFormatSymbols.getInstance(Locale.GERMANY);
    // Serialize...
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    new ObjectOutputStream(out).writeObject(originalDfs);
    byte[] bytes = out.toByteArray();
    // Deserialize...
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
    DecimalFormatSymbols deserializedDfs = (DecimalFormatSymbols) in.readObject();
    assertEquals(-1, in.read());
    // The two objects should claim to be equal.
    assertEquals(originalDfs, deserializedDfs);
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 47 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project robovm by robovm.

the class DecimalFormatTest method testSetZeroDigitForPattern.

public void testSetZeroDigitForPattern() {
    DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
    decimalFormatSymbols.setZeroDigit('a');
    DecimalFormat formatter = new DecimalFormat();
    formatter.setDecimalFormatSymbols(decimalFormatSymbols);
    formatter.applyLocalizedPattern("#.aa");
    assertEquals("e.fa", formatter.format(4.50));
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat)

Example 48 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project robovm by robovm.

the class DecimalFormatTest method testSetZeroDigitForFormatting.

public void testSetZeroDigitForFormatting() {
    DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
    decimalFormatSymbols.setZeroDigit('a');
    DecimalFormat formatter = new DecimalFormat();
    formatter.setDecimalFormatSymbols(decimalFormatSymbols);
    formatter.applyLocalizedPattern("#");
    assertEquals("eadacab", formatter.format(4030201));
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat)

Example 49 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project robovm by robovm.

the class Scanner method removeLocaleInfo.

/*
     * Remove the locale specific prefixes, group separators, and locale
     * specific suffixes from input string
     */
private String removeLocaleInfo(String token, Class<?> type) {
    DecimalFormatSymbols dfs = decimalFormat.getDecimalFormatSymbols();
    StringBuilder tokenBuilder = new StringBuilder(token);
    boolean negative = removeLocaleSign(tokenBuilder);
    // Remove group separator
    String groupSeparator = String.valueOf(dfs.getGroupingSeparator());
    int separatorIndex = -1;
    while ((separatorIndex = tokenBuilder.indexOf(groupSeparator)) != -1) {
        tokenBuilder.delete(separatorIndex, separatorIndex + 1);
    }
    // Remove decimal separator
    String decimalSeparator = String.valueOf(dfs.getDecimalSeparator());
    separatorIndex = tokenBuilder.indexOf(decimalSeparator);
    StringBuilder result = new StringBuilder("");
    if (type == int.class) {
        for (int i = 0; i < tokenBuilder.length(); i++) {
            if (Character.digit(tokenBuilder.charAt(i), Character.MAX_RADIX) != -1) {
                result.append(tokenBuilder.charAt(i));
            }
        }
    } else if (type == float.class) {
        if (tokenBuilder.toString().equals(dfs.getNaN())) {
            result.append("NaN");
        } else if (tokenBuilder.toString().equals(dfs.getInfinity())) {
            result.append("Infinity");
        } else {
            for (int i = 0; i < tokenBuilder.length(); i++) {
                if (Character.digit(tokenBuilder.charAt(i), 10) != -1) {
                    result.append(Character.digit(tokenBuilder.charAt(i), 10));
                }
            }
        }
    } else {
        throw new AssertionError("Unsupported type: " + type);
    }
    // Token is NaN or Infinity
    if (result.length() == 0) {
        result = tokenBuilder;
    }
    if (separatorIndex != -1) {
        result.insert(separatorIndex, ".");
    }
    // If input is negative
    if (negative) {
        result.insert(0, '-');
    }
    return result.toString();
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols)

Example 50 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project robovm by robovm.

the class OldDecimalFormatSymbolsTest method test_clone.

public void test_clone() {
    // case 1: Compare of internal variables of cloned objects
    DecimalFormatSymbols fs = new DecimalFormatSymbols(Locale.US);
    DecimalFormatSymbols fsc = (DecimalFormatSymbols) fs.clone();
    assertEquals(fs.getCurrency(), fsc.getCurrency());
    // case 2: Compare of clones
    fs = new DecimalFormatSymbols();
    DecimalFormatSymbols fsc2 = (DecimalFormatSymbols) (fs.clone());
    // make sure the objects are equal
    assertTrue("Object's clone isn't equal!", fs.equals(fsc2));
    // case 3:
    // change the content of the clone and make sure it's not equal
    // anymore
    // verifies that it's data is now distinct from the original
    fs.setNaN("not-a-number");
    assertTrue("Object's changed clone should not be equal!", !fs.equals(fsc2));
}
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