Search in sources :

Example 61 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project jdk8u_jdk by JetBrains.

the class RoundingAndPropertyTest method testSettersAndFastPath.

/* This methods checks the behaviour of the management of properties
     * of a DecimalFormat instance that satisfies fast-path constraints.
     *
     * It does this by comparing the results of the format(double) output
     * obtained from initial fast-path state with the output provided by
     * the same instance that has been pushed and exercised outside
     * fast-path rules and finally "reverted" to its initial fast-path state.
     *
     * The schema of actions is this :
     *  - Call format(double) on a known DecimalFormat fast-path instance,
     *    and store this result.
     *  - Record the current state of a given property.
     *  - Change the property to invalidate the fast-path state.
     *  - Call again format(double) on the instance.
     *  - Revert state of property to validate again fast-path context.
     *  - Call format(double) again.
     *  - Check that first and last call to format(double) provide same result
     *  - Record failure if any.
     *  - Do the same for another property with the same instance.
     * So all the property changes are chained one after the other on only the
     * same instance.
     *
     * Some properties that currently do not influence the fast-path state
     * are also tested. This is not useful with current fast-path source
     * but is here for testing the whole set of properties. This is the case
     * for prefixes and suffixes, and parseBigDecimal properties.
     */
private static int testSettersAndFastPath(DecimalFormat df, boolean isCurrency) {
    final double d1 = GoldenDoubleValues.PROPERTY_CHECK_POSITIVE_VALUE;
    final double d2 = GoldenDoubleValues.PROPERTY_CHECK_NEGATIVE_VALUE;
    int errors = 0;
    boolean testSucceeded = false;
    String firstFormatResult;
    String secondFormatResult;
    String propertyName;
    // ---- positivePrefix property test ----
    testSucceeded = false;
    propertyName = "positivePrefix";
    System.out.print("Checking " + propertyName + " property.");
    String initialPrefix = df.getPositivePrefix();
    firstFormatResult = df.format(d1);
    df.setPositivePrefix("positivePrefix:");
    df.format(d1);
    df.setPositivePrefix(initialPrefix);
    secondFormatResult = df.format(d1);
    testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
    if (!testSucceeded)
        errors++;
    // ---- positiveSuffix property test ----
    testSucceeded = false;
    propertyName = "positiveSuffix";
    System.out.print("Checking " + propertyName + " property.");
    String initialSuffix = df.getPositiveSuffix();
    firstFormatResult = df.format(d1);
    df.setPositiveSuffix("positiveSuffix:");
    df.format(d1);
    df.setPositiveSuffix(initialSuffix);
    secondFormatResult = df.format(d1);
    testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
    if (!testSucceeded)
        errors++;
    // ---- negativePrefix property test ----
    testSucceeded = false;
    propertyName = "negativePrefix";
    System.out.print("Checking " + propertyName + " property.");
    initialPrefix = df.getNegativePrefix();
    firstFormatResult = df.format(d1);
    df.setNegativePrefix("negativePrefix:");
    df.format(d1);
    df.setNegativePrefix(initialPrefix);
    secondFormatResult = df.format(d1);
    testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
    if (!testSucceeded)
        errors++;
    // ---- negativeSuffix property test ----
    testSucceeded = false;
    propertyName = "negativeSuffix";
    System.out.print("Checking " + propertyName + " property.");
    initialSuffix = df.getNegativeSuffix();
    firstFormatResult = df.format(d1);
    df.setNegativeSuffix("negativeSuffix:");
    df.format(d1);
    df.setNegativeSuffix(initialSuffix);
    secondFormatResult = df.format(d1);
    testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
    if (!testSucceeded)
        errors++;
    // ---- multiplier property test ----
    testSucceeded = false;
    propertyName = "multiplier";
    System.out.print("Checking " + propertyName + " property.");
    int initialMultiplier = df.getMultiplier();
    firstFormatResult = df.format(d1);
    df.setMultiplier(10);
    df.format(d1);
    df.setMultiplier(initialMultiplier);
    secondFormatResult = df.format(d1);
    testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
    if (!testSucceeded)
        errors++;
    // ---- groupingUsed property test ----
    testSucceeded = false;
    propertyName = "groupingUsed";
    System.out.print("Checking " + propertyName + " property.");
    boolean initialGroupingUsed = df.isGroupingUsed();
    firstFormatResult = df.format(d1);
    df.setGroupingUsed(!initialGroupingUsed);
    df.format(d1);
    df.setGroupingUsed(initialGroupingUsed);
    secondFormatResult = df.format(d1);
    testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
    if (!testSucceeded)
        errors++;
    // ---- groupingSize property test ----
    testSucceeded = false;
    propertyName = "groupingSize";
    System.out.print("Checking " + propertyName + " property.");
    int initialGroupingSize = df.getGroupingSize();
    firstFormatResult = df.format(d1);
    df.setGroupingSize(initialGroupingSize + 1);
    df.format(d1);
    df.setGroupingSize(initialGroupingSize);
    secondFormatResult = df.format(d1);
    testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
    if (!testSucceeded)
        errors++;
    // ---- decimalSeparatorAlwaysShown property test ----
    testSucceeded = false;
    propertyName = "decimalSeparatorAlwaysShown";
    System.out.print("Checking " + propertyName + " property.");
    boolean initialDSShown = df.isDecimalSeparatorAlwaysShown();
    firstFormatResult = df.format(d1);
    df.setDecimalSeparatorAlwaysShown(!initialDSShown);
    df.format(d1);
    df.setDecimalSeparatorAlwaysShown(initialDSShown);
    secondFormatResult = df.format(d1);
    testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
    if (!testSucceeded)
        errors++;
    // ---- parseBigDecimal property test ----
    testSucceeded = false;
    propertyName = "parseBigDecimal";
    System.out.print("Checking " + propertyName + " property.");
    boolean initialParseBigdecimal = df.isParseBigDecimal();
    firstFormatResult = df.format(d1);
    df.setParseBigDecimal(!initialParseBigdecimal);
    df.format(d1);
    df.setParseBigDecimal(initialParseBigdecimal);
    secondFormatResult = df.format(d1);
    testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
    if (!testSucceeded)
        errors++;
    // ---- maximumIntegerDigits property test ----
    testSucceeded = false;
    propertyName = "maximumIntegerDigits";
    System.out.print("Checking " + propertyName + " property.");
    int initialMaxIDs = df.getMaximumIntegerDigits();
    firstFormatResult = df.format(d1);
    df.setMaximumIntegerDigits(8);
    df.format(d1);
    df.setMaximumIntegerDigits(initialMaxIDs);
    secondFormatResult = df.format(d1);
    testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
    if (!testSucceeded)
        errors++;
    // ---- minimumIntegerDigits property test ----
    testSucceeded = false;
    propertyName = "minimumIntegerDigits";
    System.out.print("Checking " + propertyName + " property.");
    int initialMinIDs = df.getMinimumIntegerDigits();
    firstFormatResult = df.format(d1);
    df.setMinimumIntegerDigits(2);
    df.format(d1);
    df.setMinimumIntegerDigits(initialMinIDs);
    secondFormatResult = df.format(d1);
    testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
    if (!testSucceeded)
        errors++;
    // ---- maximumFractionDigits property test ----
    testSucceeded = false;
    propertyName = "maximumFractionDigits";
    System.out.print("Checking " + propertyName + " property.");
    firstFormatResult = df.format(d1);
    df.setMaximumFractionDigits(8);
    df.format(d1);
    if (isCurrency) {
        df.setMinimumFractionDigits(2);
        df.setMaximumFractionDigits(2);
    } else {
        df.setMinimumFractionDigits(0);
        df.setMaximumFractionDigits(3);
    }
    secondFormatResult = df.format(d1);
    testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
    if (!testSucceeded)
        errors++;
    // ---- minimumFractionDigits property test ----
    testSucceeded = false;
    propertyName = "minimumFractionDigits";
    System.out.print("Checking " + propertyName + " property.");
    firstFormatResult = df.format(d1);
    df.setMinimumFractionDigits(1);
    df.format(d1);
    if (isCurrency) {
        df.setMinimumFractionDigits(2);
        df.setMaximumFractionDigits(2);
    } else {
        df.setMinimumFractionDigits(0);
        df.setMaximumFractionDigits(3);
    }
    secondFormatResult = df.format(d1);
    testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
    if (!testSucceeded)
        errors++;
    // ---- currency property test ----
    testSucceeded = false;
    propertyName = "currency";
    System.out.print("Checking " + propertyName + " property.");
    Currency initialCurrency = df.getCurrency();
    Currency japanCur = java.util.Currency.getInstance(Locale.JAPAN);
    firstFormatResult = df.format(d1);
    df.setCurrency(japanCur);
    df.format(d1);
    df.setCurrency(initialCurrency);
    secondFormatResult = df.format(d1);
    testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
    if (!testSucceeded)
        errors++;
    // ---- roundingMode property test ----
    testSucceeded = false;
    propertyName = "roundingMode";
    System.out.print("Checking " + propertyName + " property.");
    RoundingMode initialRMode = df.getRoundingMode();
    firstFormatResult = df.format(d1);
    df.setRoundingMode(RoundingMode.HALF_UP);
    df.format(d1);
    df.setRoundingMode(RoundingMode.HALF_EVEN);
    secondFormatResult = df.format(d1);
    testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
    if (!testSucceeded)
        errors++;
    // ---- decimalFormatSymbols property test ----
    testSucceeded = false;
    propertyName = "decimalFormatSymbols";
    System.out.print("Checking " + propertyName + " property.");
    DecimalFormatSymbols initialDecimalFormatSymbols = df.getDecimalFormatSymbols();
    firstFormatResult = df.format(d1);
    Locale bizarreLocale = new Locale("fr", "FR");
    DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols(bizarreLocale);
    unusualSymbols.setDecimalSeparator('@');
    unusualSymbols.setGroupingSeparator('|');
    df.setDecimalFormatSymbols(unusualSymbols);
    df.format(d1);
    df.setDecimalFormatSymbols(initialDecimalFormatSymbols);
    secondFormatResult = df.format(d1);
    testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
    if (!testSucceeded)
        errors++;
    testSucceeded = false;
    System.out.print("Checking " + propertyName + " property.");
    initialDecimalFormatSymbols = df.getDecimalFormatSymbols();
    firstFormatResult = df.format(d1);
    Locale japanLocale = Locale.JAPAN;
    unusualSymbols = new DecimalFormatSymbols(japanLocale);
    unusualSymbols.setDecimalSeparator('9');
    unusualSymbols.setGroupingSeparator('0');
    df.setDecimalFormatSymbols(unusualSymbols);
    df.format(d1);
    df.setDecimalFormatSymbols(initialDecimalFormatSymbols);
    secondFormatResult = df.format(d1);
    testSucceeded = resultsEqual(propertyName, firstFormatResult, secondFormatResult);
    if (!testSucceeded)
        errors++;
    return errors;
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) RoundingMode(java.math.RoundingMode)

Example 62 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project jdk8u_jdk by JetBrains.

the class LocaleEnhanceTest method checkDigit.

private void checkDigit(Locale loc, Character expected) {
    DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(loc);
    Character zero = dfs.getZeroDigit();
    assertEquals("Wrong digit zero char", expected, zero);
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols)

Example 63 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project opennms by OpenNMS.

the class SIUtilsTest method testTwelveHundred.

/**
 * 1200bps modem line.
 */
public void testTwelveHundred() {
    DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.getDefault());
    assertEquals("1" + symbols.getDecimalSeparator() + "2 kbps", SIUtils.getHumanReadableIfSpeed(1200L));
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols)

Example 64 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project nem2-sdk-java by nemtech.

the class FormatUtils method getDefaultDecimalFormat.

/**
 * Gets a default decimal format that should be used for formatting decimal values.
 *
 * @return A default decimal format.
 */
public static DecimalFormat getDefaultDecimalFormat() {
    final DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
    decimalFormatSymbols.setDecimalSeparator('.');
    final DecimalFormat format = new DecimalFormat("#0.000", decimalFormatSymbols);
    format.setGroupingUsed(false);
    return format;
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat)

Example 65 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project opennms by OpenNMS.

the class SIUtilsTest method testOnePointFiveFourFourMeg.

/**
 * DS-1 line speed.
 * @link http://en.wikipedia.org/wiki/Digital_Signal_1
 */
public void testOnePointFiveFourFourMeg() {
    DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.getDefault());
    assertEquals("1" + symbols.getDecimalSeparator() + "544 Mbps", SIUtils.getHumanReadableIfSpeed(1544000L));
}
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