Package com.ibm.icu.text

Examples of com.ibm.icu.text.NumberFormat


    }

    public void TestNumberFormat() {
        GlobalizationPreferences gp = new GlobalizationPreferences();

        NumberFormat nf;
        String numStr;
        double num = 123456.789;

        // Set unsupported locale with supported territory ang_KR
        logln("Set locale - ang_KR");
        gp.setLocale(new ULocale("ang_KR"));
        nf = gp.getNumberFormat(GlobalizationPreferences.NF_CURRENCY);
        numStr = nf.format(num);
        if (!numStr.equals("\u20a9\u00a0123,457")) {
            errln("FAIL: Number string is " + numStr + " Expected: \u20a9\u00a0123,457");
        }
       
        // Set locale - de_DE
        logln("Set locale - de_DE");
        gp.setLocale(new ULocale("de_DE"));

        // NF_NUMBER
        logln("NUMBER type");
        nf = gp.getNumberFormat(GlobalizationPreferences.NF_NUMBER);
        numStr = nf.format(num);
        if (!numStr.equals("123.456,789")) {
            errln("FAIL: Number string is " + numStr + " Expected: 123.456,789");
        }

        // NF_CURRENCY
        logln("CURRENCY type");
        nf = gp.getNumberFormat(GlobalizationPreferences.NF_CURRENCY);
        numStr = nf.format(num);
        if (!numStr.equals("123.456,79\u00a0\u20AC")) {
            errln("FAIL: Number string is " + numStr + " Expected: 123.456,79\u00a0\u20AC");
        }

        // NF_PERCENT
        logln("PERCENT type");
        nf = gp.getNumberFormat(GlobalizationPreferences.NF_PERCENT);
        numStr = nf.format(num);
        if (!numStr.equals("12.345.679\u00a0%")) {
            errln("FAIL: Number string is " + numStr + " Expected: 12.345.679\u00a0%");
        }

        // NF_SCIENTIFIC
        logln("SCIENTIFIC type");
        nf = gp.getNumberFormat(GlobalizationPreferences.NF_SCIENTIFIC);
        numStr = nf.format(num);
        if (!numStr.equals("1,23456789E5")) {
            errln("FAIL: Number string is " + numStr + " Expected: 1,23456789E5");
        }

        // NF_INTEGER
        logln("INTEGER type");
        nf = gp.getNumberFormat(GlobalizationPreferences.NF_INTEGER);
        numStr = nf.format(num);
        if (!numStr.equals("123.457")) {
            errln("FAIL: Number string is " + numStr + " Expected: 123.457");
        }

        // Invalid number type
        logln("INVALID type");
        boolean illegalArg = false;
        try {
            nf = gp.getNumberFormat(100);
        } catch (IllegalArgumentException iae) {
            logln("Illegal number format type 100");
            illegalArg = true;
        }
        if (!illegalArg) {
            errln("FAIL: getNumberFormat must throw IllegalArgumentException for type 100");
        }
        illegalArg = false;
        try {
            nf = gp.getNumberFormat(-1);
        } catch (IllegalArgumentException iae) {
            logln("Illegal number format type -1");
            illegalArg = true;
        }
        if (!illegalArg) {
            errln("FAIL: getNumberFormat must throw IllegalArgumentException for type -1");
        }
       
        // Set explicit territory
        logln("Set territory - US");
        gp.setTerritory("US");
        nf = gp.getNumberFormat(GlobalizationPreferences.NF_CURRENCY);
        numStr = nf.format(num);
        if (!numStr.equals("123.456,79\u00a0$")) {
            errln("FAIL: Number string is " + numStr + " Expected: 123.456,79\u00a0$");
        }

        // Set explicit currency
        logln("Set currency - GBP");
        gp.setCurrency(Currency.getInstance("GBP"));
        nf = gp.getNumberFormat(GlobalizationPreferences.NF_CURRENCY);
        numStr = nf.format(num);
        if (!numStr.equals("123.456,79\u00a0\u00A3")) {
            errln("FAIL: Number string is " + numStr + " Expected: 123.456,79\u00a0\u00A3");
        }

        // Set exliplicit NumberFormat
        logln("Set explicit NumberFormat objects");
        NumberFormat customNum = NumberFormat.getNumberInstance(new ULocale("he_IL"));
        gp.setNumberFormat(GlobalizationPreferences.NF_NUMBER, customNum);
        NumberFormat customCur = NumberFormat.getCurrencyInstance(new ULocale("zh_CN"));
        gp.setNumberFormat(GlobalizationPreferences.NF_CURRENCY, customCur);
        NumberFormat customPct = NumberFormat.getPercentInstance(new ULocale("el_GR"));
        gp.setNumberFormat(GlobalizationPreferences.NF_PERCENT, customPct);
        NumberFormat customSci = NumberFormat.getScientificInstance(new ULocale("ru_RU"));
        gp.setNumberFormat(GlobalizationPreferences.NF_SCIENTIFIC, customSci);
        NumberFormat customInt = NumberFormat.getIntegerInstance(new ULocale("pt_PT"));
        gp.setNumberFormat(GlobalizationPreferences.NF_INTEGER, customInt);

       
        nf = gp.getNumberFormat(GlobalizationPreferences.NF_NUMBER);
        if (!nf.getLocale(ULocale.VALID_LOCALE).toString().equals("he_IL")) {
            errln("FAIL: The NumberFormat instance must use locale he_IL");
        }
        nf = gp.getNumberFormat(GlobalizationPreferences.NF_CURRENCY);
        if (!nf.getLocale(ULocale.VALID_LOCALE).toString().equals("zh_CN")) {
            errln("FAIL: The NumberFormat instance must use locale zh_CN");
        }
        nf = gp.getNumberFormat(GlobalizationPreferences.NF_PERCENT);
        if (!nf.getLocale(ULocale.VALID_LOCALE).toString().equals("el_GR")) {
            errln("FAIL: The NumberFormat instance must use locale el_GR");
        }
        nf = gp.getNumberFormat(GlobalizationPreferences.NF_SCIENTIFIC);
        if (!nf.getLocale(ULocale.VALID_LOCALE).toString().equals("ru_RU")) {
            errln("FAIL: The NumberFormat instance must use locale ru_RU");
        }
        nf = gp.getNumberFormat(GlobalizationPreferences.NF_INTEGER);
        if (!nf.getLocale(ULocale.VALID_LOCALE).toString().equals("pt_PT")) {
            errln("FAIL: The NumberFormat instance must use locale pt_PT");
        }

        NumberFormat customNum1 = NumberFormat.getNumberInstance(new ULocale("hi_IN"));

        // Freeze
        logln("Freeze this object");
        boolean isFrozen = false;
        gp.freeze();
View Full Code Here


    /*
     * Test method for 'com.ibm.icu.x.text.NumberFormat.getIntegerInstance(ULocale)'
     */
    public void testGetIntegerInstanceULocale() {
        NumberFormat nf = NumberFormat.getIntegerInstance(ULocale.GERMANY);
        assertEquals("123.457", nf.format(123456.789)); // rounds
    }
View Full Code Here

    /*
     * Test method for 'com.ibm.icu.x.text.NumberFormat.getCurrencyInstance()'
     */
    public void testGetCurrencyInstance() {
        NumberFormat nf = NumberFormat.getCurrencyInstance();
        assertEquals("$123,456.99", nf.format(123456.99));
    }
View Full Code Here

    /*
     * Test method for 'com.ibm.icu.x.text.NumberFormat.getCurrencyInstance(Locale)'
     */
    public void testGetCurrencyInstanceLocale() {
        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.GERMANY);
        assertEquals("123.456,99 \u20AC", nf.format(123456.99));
    }
View Full Code Here

    /*
     * Test method for 'com.ibm.icu.x.text.NumberFormat.getCurrencyInstance(ULocale)'
     */
    public void testGetCurrencyInstanceULocale() {
        NumberFormat nf = NumberFormat.getCurrencyInstance(ULocale.GERMANY);
        assertEquals("123.456,99 \u20AC", nf.format(123456.99));
    }
View Full Code Here

    /*
     * Test method for 'com.ibm.icu.x.text.NumberFormat.getPercentInstance()'
     */
    public void testGetPercentInstance() {
        NumberFormat nf = NumberFormat.getPercentInstance();
        assertEquals("123,456%", nf.format(1234.56));
    }
View Full Code Here

    /*
     * Test method for 'com.ibm.icu.x.text.NumberFormat.getPercentInstance(Locale)'
     */
    public void testGetPercentInstanceLocale() {
        NumberFormat nf = NumberFormat.getPercentInstance(Locale.GERMANY);
        assertEquals("123.456%", nf.format(1234.56));
    }
View Full Code Here

    /*
     * Test method for 'com.ibm.icu.x.text.NumberFormat.getPercentInstance(ULocale)'
     */
    public void testGetPercentInstanceULocale() {
        NumberFormat nf = NumberFormat.getPercentInstance(ULocale.GERMANY);
        assertEquals("123.456%", nf.format(1234.56));
    }
View Full Code Here

    /*
     * Test method for 'com.ibm.icu.x.text.NumberFormat.getScientificInstance()'
     */
    public void testGetScientificInstance() {
        NumberFormat nf = NumberFormat.getScientificInstance();
        assertEquals(".123456E4", nf.format(1234.56));
    }
View Full Code Here

    /*
     * Test method for 'com.ibm.icu.x.text.NumberFormat.getScientificInstance(Locale)'
     */
    public void testGetScientificInstanceLocale() {
        NumberFormat nf = NumberFormat.getScientificInstance(Locale.GERMANY);
        assertEquals(",123456E4", nf.format(1234.56));
    }
View Full Code Here

TOP

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

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.