Search in sources :

Example 1 with DecimalFormat

use of android.icu.text.DecimalFormat in project android_packages_apps_Settings by DirtyUnicorns.

the class FileSizeFormatter method getNumberFormatter.

private static NumberFormat getNumberFormatter(Locale locale, int fractionDigits) {
    final NumberFormat numberFormatter = NumberFormat.getInstance(locale);
    numberFormatter.setMinimumFractionDigits(fractionDigits);
    numberFormatter.setMaximumFractionDigits(fractionDigits);
    numberFormatter.setGroupingUsed(false);
    if (numberFormatter instanceof DecimalFormat) {
        // We do this only for DecimalFormat, since in the general NumberFormat case, calling
        // setRoundingMode may throw an exception.
        numberFormatter.setRoundingMode(BigDecimal.ROUND_HALF_UP);
    }
    return numberFormatter;
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) NumberFormat(android.icu.text.NumberFormat)

Example 2 with DecimalFormat

use of android.icu.text.DecimalFormat in project platform_packages_apps_Settings by BlissRoms.

the class FileSizeFormatter method getNumberFormatter.

private static NumberFormat getNumberFormatter(Locale locale, int fractionDigits) {
    final NumberFormat numberFormatter = NumberFormat.getInstance(locale);
    numberFormatter.setMinimumFractionDigits(fractionDigits);
    numberFormatter.setMaximumFractionDigits(fractionDigits);
    numberFormatter.setGroupingUsed(false);
    if (numberFormatter instanceof DecimalFormat) {
        // We do this only for DecimalFormat, since in the general NumberFormat case, calling
        // setRoundingMode may throw an exception.
        numberFormatter.setRoundingMode(BigDecimal.ROUND_HALF_UP);
    }
    return numberFormatter;
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) NumberFormat(android.icu.text.NumberFormat)

Example 3 with DecimalFormat

use of android.icu.text.DecimalFormat in project j2objc by google.

the class IntlTestDecimalFormatAPI method testJB4971.

@Test
public void testJB4971() {
    DecimalFormat decfmt = new DecimalFormat();
    MathContext resultICU;
    MathContext comp1 = new MathContext(0, MathContext.PLAIN);
    resultICU = decfmt.getMathContextICU();
    if ((comp1.getDigits() != resultICU.getDigits()) || (comp1.getForm() != resultICU.getForm()) || (comp1.getLostDigits() != resultICU.getLostDigits()) || (comp1.getRoundingMode() != resultICU.getRoundingMode())) {
        errln("ERROR: Math context 1 not equal - result: " + resultICU.toString() + " / expected: " + comp1.toString());
    }
    MathContext comp2 = new MathContext(5, MathContext.ENGINEERING);
    decfmt.setMathContextICU(comp2);
    resultICU = decfmt.getMathContextICU();
    if ((comp2.getDigits() != resultICU.getDigits()) || (comp2.getForm() != resultICU.getForm()) || (comp2.getLostDigits() != resultICU.getLostDigits()) || (comp2.getRoundingMode() != resultICU.getRoundingMode())) {
        errln("ERROR: Math context 2 not equal - result: " + resultICU.toString() + " / expected: " + comp2.toString());
    }
    java.math.MathContext result;
    java.math.MathContext comp3 = new java.math.MathContext(3, java.math.RoundingMode.DOWN);
    decfmt.setMathContext(comp3);
    result = decfmt.getMathContext();
    if ((comp3.getPrecision() != result.getPrecision()) || (comp3.getRoundingMode() != result.getRoundingMode())) {
        errln("ERROR: Math context 3 not equal - result: " + result.toString() + " / expected: " + comp3.toString());
    }
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) MathContext(android.icu.math.MathContext) Test(org.junit.Test)

Example 4 with DecimalFormat

use of android.icu.text.DecimalFormat in project j2objc by google.

the class IntlTestDecimalFormatAPI method testJB6354.

@Test
public void testJB6354() {
    DecimalFormat pat = new DecimalFormat("#,##0.00");
    java.math.BigDecimal r1, r2;
    // get default rounding increment
    r1 = pat.getRoundingIncrement();
    // set rounding mode with zero increment.  Rounding
    // increment should be set by this operation
    pat.setRoundingMode(BigDecimal.ROUND_UP);
    r2 = pat.getRoundingIncrement();
    // check for different values
    if ((r1 != null) && (r2 != null)) {
        if (r1.compareTo(r2) == 0) {
            errln("ERROR: Rounding increment did not change");
        }
    }
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 5 with DecimalFormat

use of android.icu.text.DecimalFormat in project j2objc by google.

the class IntlTestDecimalFormatAPI method TestAPI.

/**
 * This test checks various generic API methods in DecimalFormat to achieve
 * 100% API coverage.
 */
@Test
public void TestAPI() {
    logln("DecimalFormat API test---");
    logln("");
    Locale.setDefault(Locale.ENGLISH);
    // ======= Test constructors
    logln("Testing DecimalFormat constructors");
    DecimalFormat def = new DecimalFormat();
    final String pattern = new String("#,##0.# FF");
    DecimalFormat pat = null;
    try {
        pat = new DecimalFormat(pattern);
    } catch (IllegalArgumentException e) {
        errln("ERROR: Could not create DecimalFormat (pattern)");
    }
    DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.FRENCH);
    DecimalFormat cust1 = new DecimalFormat(pattern, symbols);
    // ======= Test clone(), assignment, and equality
    logln("Testing clone() and equality operators");
    Format clone = (Format) def.clone();
    if (!def.equals(clone)) {
        errln("ERROR: Clone() failed");
    }
    // ======= Test various format() methods
    logln("Testing various format() methods");
    // final double d = -10456.0037; // this appears as -10456.003700000001 on NT
    // final double d = -1.04560037e-4; // this appears as -1.0456003700000002E-4 on NT
    // this works!
    final double d = -10456.00370000000000;
    final long l = 100000000;
    logln("" + d + " is the double value");
    StringBuffer res1 = new StringBuffer();
    StringBuffer res2 = new StringBuffer();
    StringBuffer res3 = new StringBuffer();
    StringBuffer res4 = new StringBuffer();
    FieldPosition pos1 = new FieldPosition(0);
    FieldPosition pos2 = new FieldPosition(0);
    FieldPosition pos3 = new FieldPosition(0);
    FieldPosition pos4 = new FieldPosition(0);
    res1 = def.format(d, res1, pos1);
    logln("" + d + " formatted to " + res1);
    res2 = pat.format(l, res2, pos2);
    logln("" + l + " formatted to " + res2);
    res3 = cust1.format(d, res3, pos3);
    logln("" + d + " formatted to " + res3);
    res4 = cust1.format(l, res4, pos4);
    logln("" + l + " formatted to " + res4);
    // ======= Test parse()
    logln("Testing parse()");
    String text = new String("-10,456.0037");
    ParsePosition pos = new ParsePosition(0);
    String patt = new String("#,##0.#");
    pat.applyPattern(patt);
    double d2 = pat.parse(text, pos).doubleValue();
    if (d2 != d) {
        errln("ERROR: Roundtrip failed (via parse(" + d2 + " != " + d + ")) for " + text);
    }
    logln(text + " parsed into " + (long) d2);
    // ======= Test getters and setters
    logln("Testing getters and setters");
    final DecimalFormatSymbols syms = pat.getDecimalFormatSymbols();
    def.setDecimalFormatSymbols(syms);
    if (!pat.getDecimalFormatSymbols().equals(def.getDecimalFormatSymbols())) {
        errln("ERROR: set DecimalFormatSymbols() failed");
    }
    String posPrefix;
    pat.setPositivePrefix("+");
    posPrefix = pat.getPositivePrefix();
    logln("Positive prefix (should be +): " + posPrefix);
    if (posPrefix != "+") {
        errln("ERROR: setPositivePrefix() failed");
    }
    String negPrefix;
    pat.setNegativePrefix("-");
    negPrefix = pat.getNegativePrefix();
    logln("Negative prefix (should be -): " + negPrefix);
    if (negPrefix != "-") {
        errln("ERROR: setNegativePrefix() failed");
    }
    String posSuffix;
    pat.setPositiveSuffix("_");
    posSuffix = pat.getPositiveSuffix();
    logln("Positive suffix (should be _): " + posSuffix);
    if (posSuffix != "_") {
        errln("ERROR: setPositiveSuffix() failed");
    }
    String negSuffix;
    pat.setNegativeSuffix("~");
    negSuffix = pat.getNegativeSuffix();
    logln("Negative suffix (should be ~): " + negSuffix);
    if (negSuffix != "~") {
        errln("ERROR: setNegativeSuffix() failed");
    }
    long multiplier = 0;
    pat.setMultiplier(8);
    multiplier = pat.getMultiplier();
    logln("Multiplier (should be 8): " + multiplier);
    if (multiplier != 8) {
        errln("ERROR: setMultiplier() failed");
    }
    int groupingSize = 0;
    pat.setGroupingSize(2);
    groupingSize = pat.getGroupingSize();
    logln("Grouping size (should be 2): " + (long) groupingSize);
    if (groupingSize != 2) {
        errln("ERROR: setGroupingSize() failed");
    }
    pat.setDecimalSeparatorAlwaysShown(true);
    boolean tf = pat.isDecimalSeparatorAlwaysShown();
    logln("DecimalSeparatorIsAlwaysShown (should be true) is " + (tf ? "true" : "false"));
    if (tf != true) {
        errln("ERROR: setDecimalSeparatorAlwaysShown() failed");
    }
    String funkyPat;
    funkyPat = pat.toPattern();
    logln("Pattern is " + funkyPat);
    String locPat;
    locPat = pat.toLocalizedPattern();
    logln("Localized pattern is " + locPat);
    // ======= Test applyPattern()
    logln("Testing applyPattern()");
    String p1 = new String("#,##0.0#;(#,##0.0#)");
    logln("Applying pattern " + p1);
    pat.applyPattern(p1);
    String s2;
    s2 = pat.toPattern();
    logln("Extracted pattern is " + s2);
    if (!s2.equals(p1)) {
        errln("ERROR: toPattern() result did not match pattern applied");
    }
    String p2 = new String("#,##0.0# FF;(#,##0.0# FF)");
    logln("Applying pattern " + p2);
    pat.applyLocalizedPattern(p2);
    String s3;
    s3 = pat.toLocalizedPattern();
    logln("Extracted pattern is " + s3);
    if (!s3.equals(p2)) {
        errln("ERROR: toLocalizedPattern() result did not match pattern applied");
    }
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) Format(java.text.Format) NumberFormat(android.icu.text.NumberFormat) DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) DecimalFormat(android.icu.text.DecimalFormat) FieldPosition(java.text.FieldPosition) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Aggregations

DecimalFormat (android.icu.text.DecimalFormat)150 Test (org.junit.Test)138 CompactDecimalFormat (android.icu.text.CompactDecimalFormat)70 DecimalFormatSymbols (android.icu.text.DecimalFormatSymbols)57 NumberFormat (android.icu.text.NumberFormat)30 ULocale (android.icu.util.ULocale)28 FieldPosition (java.text.FieldPosition)25 ParseException (java.text.ParseException)23 Locale (java.util.Locale)18 ParsePosition (java.text.ParsePosition)15 RuleBasedNumberFormat (android.icu.text.RuleBasedNumberFormat)12 BigDecimal (android.icu.math.BigDecimal)10 IOException (java.io.IOException)8 InvalidObjectException (java.io.InvalidObjectException)5 BigInteger (java.math.BigInteger)5 ScientificNumberFormatter (android.icu.text.ScientificNumberFormatter)4 SimpleDateFormat (android.icu.text.SimpleDateFormat)4 Format (java.text.Format)4 DateFormat (android.icu.text.DateFormat)3 MessageFormat (android.icu.text.MessageFormat)3