Package com.ibm.icu.text

Examples of com.ibm.icu.text.DecimalFormat


        // problem 2
        double number = 8.88885;
        String expected = "8.8889";
       
        String pat = ",##0.0000";
        DecimalFormat dec = new DecimalFormat(pat);
        dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);
        double roundinginc = 0.0001;
        dec.setRoundingIncrement(roundinginc);
        String str = dec.format(number);
        if (!str.equals(expected)) {
            errln("Fail: " + number + " x \"" + pat + "\" = \"" +
                  str + "\", expected \"" + expected + "\"");
        }  

        pat = ",##0.0001";
        dec = new DecimalFormat(pat);
        dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);
        str = dec.format(number);
        if (!str.equals(expected)) {
            errln("Fail: " + number + " x \"" + pat + "\" = \"" +
                  str + "\", expected \"" + expected + "\"");
       
       
        // testing 20 decimal places
        pat = ",##0.00000000000000000001";
        dec = new DecimalFormat(pat);
        BigDecimal bignumber = new BigDecimal("8.888888888888888888885");
        expected = "8.88888888888888888889";
       
        dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);
        str = dec.format(bignumber);
        if (!str.equals(expected)) {
            errln("Fail: " + bignumber + " x \"" + pat + "\" = \"" +
                  str + "\", expected \"" + expected + "\"");
        }  
       
View Full Code Here


        // ======= 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
        final double d = -10456.00370000000000; // this works!
        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()");
View Full Code Here

//#if defined(FOUNDATION10) || defined(J2SE13)
//#else
    public void testJB6134()
    {
        DecimalFormat decfmt = new DecimalFormat();
        StringBuffer buf = new StringBuffer();

        FieldPosition fposByInt = new FieldPosition(NumberFormat.INTEGER_FIELD);
        decfmt.format(123, buf, fposByInt);

        buf.setLength(0);
        FieldPosition fposByField = new FieldPosition(NumberFormat.Field.INTEGER);
        decfmt.format(123, buf, fposByField);

        if (fposByInt.getEndIndex() != fposByField.getEndIndex())
        {
            errln("ERROR: End index for integer field - fposByInt:" + fposByInt.getEndIndex() +
                " / fposByField: " + fposByField.getEndIndex());
View Full Code Here

    }
//#endif

    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());
        }

//#if defined(FOUNDATION10) || defined(J2SE13) || defined(J2SE14)
//#else

        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());
View Full Code Here

    }

    public void testJB6354()
    {
        DecimalFormat pat = new DecimalFormat("#,##0.00");

        // get default rounding increment
//#if defined(FOUNDATION10)
//##        com.ibm.icu.math.BigDecimal r1 = pat.getRoundingIncrement();
//#else
        java.math.BigDecimal r1 = pat.getRoundingIncrement();
//#endif

        // set rounding mode with zero increment.  Rounding
        // increment should be set by this operation
        pat.setRoundingMode(BigDecimal.ROUND_UP);
//#if defined(FOUNDATION10)
//##        com.ibm.icu.math.BigDecimal r2 = pat.getRoundingIncrement();
//#else
        java.math.BigDecimal r2 = pat.getRoundingIncrement();
//#endif

        // check for different values
        if ((r1 != null) && (r2 != null))
        {
View Full Code Here

        }
    }
   
    public void testJB6648()
    {
        DecimalFormat df = new DecimalFormat();
        df.setParseStrict(true);
       
        String numstr = new String();
       
        String[] patterns = {
            "0",
            "00",
            "000",
            "0,000",
            "0.0",
            "#000.0"         
        };
       
        for(int i=0; i < patterns.length; i++) {
            df.applyPattern(patterns[i]);
            numstr = df.format(5);       
            try {
                Number n = df.parse(numstr);
                logln("INFO: Parsed " + numstr + " -> " + n);
            } catch (ParseException pe) {
                errln("ERROR: Failed round trip with strict parsing.");
            }          
        }
       
        df.applyPattern(patterns[1]);
        numstr = "005";       
        try {
            Number n = df.parse(numstr);
            errln("ERROR: Expected round trip failure not encountered: numstr -> " + n);
        } catch (ParseException pe) {
            logln("INFO: Expected ParseExpection for " + numstr + " with strick parse enabled");
       
       
View Full Code Here

                    ((SimpleDateFormat) uFormat).setTimeZone(TimeZone.getTimeZone("GMT"));
                    uFormat.format(new DateTimeValue(1995, 7, 3, 2, 59, 12, 123).getObjectToFormat());
                    break;
                case NUMBER:
                    DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
                    uFormat = new DecimalFormat(pattern, symbols);
                    uFormat.format(new NumberValue(-12.3).getObjectToFormat());
                    break;
            }
        }
        catch(RuntimeException e) {
View Full Code Here

    }

    public static String formatNumber(Object data) {

        DecimalFormat numberFormat = new DecimalFormat("0.00E00");
        return numberFormat.format((Number) data);

    }
View Full Code Here

  // Don't use parentheses to indicate negative amounts. It just confuses old
  // people.
  private static class USFormatBuilder implements MoneyFormatBuilder {
    @Override
    public NumberFormat build(Currency currency) {
      final DecimalFormat nf = (DecimalFormat) NumberFormat.getCurrencyInstance(Locale.US);
      nf.setCurrency(getICUCurrency(currency));
      nf.setNegativePrefix(nf.getNegativePrefix().replace("(", "-"));
      nf.setNegativeSuffix("");
      nf.setRoundingIncrement(0.0);
     
      return nf;
    }
View Full Code Here

        private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd EEE HH:mm:ss", ULocale.US);
        private DecimalFormat decf;

        public DumpFormatter() {
            DecimalFormatSymbols decfs = new DecimalFormatSymbols(ULocale.US);
            decf = new DecimalFormat("00", decfs);
        }
View Full Code Here

TOP

Related Classes of com.ibm.icu.text.DecimalFormat

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.