Java Code Examples for android.icu.text.NumberFormat#getScientificInstance()

The following examples show how to use android.icu.text.NumberFormat#getScientificInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BigNumberFormatTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestBigDecimalJ28() {
    String[] DATA = {
        "1", "1E0",
        "-1", "-1E0",
        "0", "0E0",
        "12e34", "1.2E35",
        "-12.3e-45", "-1.23E-44",
        "0.73e-7", "7.3E-8",
    };
    NumberFormat fmt = NumberFormat.getScientificInstance(Locale.US);
    logln("Pattern: " + ((DecimalFormat)fmt).toPattern());
    for (int i=0; i<DATA.length; i+=2) {
        String input = DATA[i];
        String exp = DATA[i+1];
        android.icu.math.BigDecimal bd = new android.icu.math.BigDecimal(input);
        String output = fmt.format(bd);
        if (output.equals(exp)) {
            logln("input=" + input + " num=" + bd + " output=" + output);
        } else {
            errln("FAIL: input=" + input + " num=" + bd + " output=" + output +
                  " expected=" + exp);
        }
    }
}
 
Example 2
Source File: IntlTestNumberFormat.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Internal use
 */
private void _testLocale(Locale locale) {
    String localeName = locale + " (" + locale.getDisplayName() + ")";
        
    logln("Number test " + localeName);
    fNumberFormat = NumberFormat.getInstance(locale);
    _testFormat();

    logln("Currency test " + localeName);
    fNumberFormat = NumberFormat.getCurrencyInstance(locale);
    _testFormat();

    logln("Percent test " + localeName);
    fNumberFormat = NumberFormat.getPercentInstance(locale);
    _testFormat();

    if (locale.toString().compareTo("en_US_POSIX") != 0 ) {
       logln("Scientific test " + localeName);
       fNumberFormat = NumberFormat.getScientificInstance(locale);
       _testFormat();
    }
}
 
Example 3
Source File: WriteNumberFormatSerialTestData.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args){
    NumberFormat nf     = NumberFormat.getInstance(Locale.US);
    NumberFormat nfc    = NumberFormat.getCurrencyInstance(Locale.US);
    NumberFormat nfp     = NumberFormat.getPercentInstance(Locale.US);
    NumberFormat nfsp     = NumberFormat.getScientificInstance(Locale.US);
    
    try{
        FileOutputStream file = new FileOutputStream("NumberFormatSerialTestData.java");
        file.write(header.getBytes());
        write(file,(Object)nf,"generalInstance", "//NumberFormat.getInstance(Locale.US)");
        write(file,(Object)nfc,"currencyInstance","//NumberFormat.getCurrencyInstance(Locale.US)");
        write(file,(Object)nfp,"percentInstance","//NumberFormat.getPercentInstance(Locale.US)");
        write(file,(Object)nfsp,"scientificInstance","//NumberFormat.getScientificInstance(Locale.US)");
        file.write(footer.getBytes());            
        file.close();
    }catch( Exception e){
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
 
Example 4
Source File: GlobalizationPreferences.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * This function can be overridden by subclasses to use different heuristics.
 * <b>It MUST return a 'safe' value,
 * one whose modification will not affect this object.</b>
 *
 * @param style
 * @hide draft / provisional / internal are hidden on Android
 */
protected NumberFormat guessNumberFormat(int style) {
    NumberFormat result;
    ULocale nfLocale = getAvailableLocale(TYPE_NUMBERFORMAT);
    if (nfLocale == null) {
        nfLocale = ULocale.ROOT;
    }
    switch (style) {
    case NF_NUMBER:
        result = NumberFormat.getInstance(nfLocale);
        break;
    case NF_SCIENTIFIC:
        result = NumberFormat.getScientificInstance(nfLocale);
        break;
    case NF_INTEGER:
        result = NumberFormat.getIntegerInstance(nfLocale);
        break;
    case NF_PERCENT:
        result = NumberFormat.getPercentInstance(nfLocale);
        break;
    case NF_CURRENCY:
        result = NumberFormat.getCurrencyInstance(nfLocale);
        result.setCurrency(getCurrency());
        break;
    default:
        throw new IllegalArgumentException("Unknown number format style");
    }
    return result;
}
 
Example 5
Source File: ScientificNumberFormatterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestPlusSignInExponentMarkup() {
    DecimalFormat decfmt = (DecimalFormat) NumberFormat.getScientificInstance(ULocale.ENGLISH);
    decfmt.applyPattern("0.00E+0");
    ScientificNumberFormatter fmt = ScientificNumberFormatter.getMarkupInstance(
            decfmt, "<sup>", "</sup>");
            
    assertEquals(
            "",
            "6.02×10<sup>+23</sup>",
            fmt.format(6.02e23));
}
 
Example 6
Source File: ScientificNumberFormatterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestPlusSignInExponentSuperscript() {
    DecimalFormat decfmt = (DecimalFormat) NumberFormat.getScientificInstance(ULocale.ENGLISH);
    decfmt.applyPattern("0.00E+0");
    ScientificNumberFormatter fmt = ScientificNumberFormatter.getSuperscriptInstance(
            decfmt);
    assertEquals(
            "",
            "6.02×10⁺²³",
            fmt.format(6.02e23));
}
 
Example 7
Source File: TestCLDRVsICU.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void handleResult(ULocale locale, String result) {
    NumberFormat nf = null;
    double v = Double.NaN;
    for (Iterator it = settings.keySet().iterator(); it.hasNext();) {
        String attributeName = (String) it.next();
        String attributeValue = (String) settings.get(attributeName);

        // Checks if the attribute name is a draft and whether
        // or not it has been approved / contributed by CLDR yet
        // otherwise, skips it because it is most likely rejected by ICU
        if (attributeName.equals("draft")) {
            if (attributeValue.indexOf("approved") == -1 && attributeValue.indexOf("contributed") == -1) {
                break;
            }
            continue;
        }

        // Update the value to be checked
        if (attributeName.equals("input")) {
            v = Double.parseDouble(attributeValue);
            continue;
        }

        // At this point, it must be a numberType
        int index = lookupValue(attributeValue, NumberNames);

        if (DEBUG)
            logln("Getting number format for " + locale);
        switch (index) {
        case 0:
            nf = NumberFormat.getInstance(locale);
            break;
        case 1:
            nf = NumberFormat.getIntegerInstance(locale);
            break;
        case 2:
            nf = NumberFormat.getNumberInstance(locale);
            break;
        case 3:
            nf = NumberFormat.getPercentInstance(locale);
            break;
        case 4:
            nf = NumberFormat.getScientificInstance(locale);
            break;
        default:
            nf = NumberFormat.getCurrencyInstance(locale);
            nf.setCurrency(Currency.getInstance(attributeValue));
            break;
        }
        String temp = nf.format(v).trim();
        result = result.trim(); // HACK because of SAX
        if (!temp.equals(result)) {
            logln("Number: Locale: " + locale +
                    "\n\tType: " + attributeValue +
                    "\n\tDraft: " + settings.get("draft") +
                    "\n\tCLDR: <" + result + ">" +
                    "\n\tICU: <" + temp + ">");
        }

    }
}