Search in sources :

Example 6 with NumberFormat

use of com.ibm.icu.text.NumberFormat in project es6draft by anba.

the class NumberFormatConstructor method PartitionNumberPattern.

/**
 * PartitionNumberPattern(numberFormat, x)
 *
 * @param numberFormatObj
 *            the number format object
 * @param x
 *            the number value
 * @return the formatted number object
 */
private static List<Map.Entry<String, String>> PartitionNumberPattern(NumberFormatObject numberFormat, double x) {
    if (x == -0.0) {
        // -0 is not considered to be negative, cf. step 3a
        x = +0.0;
    }
    ArrayList<Map.Entry<String, String>> parts = new ArrayList<>();
    NumberFormat numFormat = numberFormat.getNumberFormat();
    AttributedCharacterIterator iterator = numFormat.formatToCharacterIterator(x);
    StringBuilder sb = new StringBuilder();
    for (char ch = iterator.first(); ch != CharacterIterator.DONE; ch = iterator.next()) {
        sb.append(ch);
        if (iterator.getIndex() + 1 == iterator.getRunLimit()) {
            Iterator<Attribute> keyIterator = iterator.getAttributes().keySet().iterator();
            String key;
            if (keyIterator.hasNext()) {
                key = fieldToString((NumberFormat.Field) keyIterator.next(), x);
            } else {
                key = "literal";
            }
            String value = sb.toString();
            sb.setLength(0);
            parts.add(new AbstractMap.SimpleImmutableEntry<>(key, value));
        }
    }
    return parts;
}
Also used : Attribute(java.text.AttributedCharacterIterator.Attribute) ArrayList(java.util.ArrayList) AttributedCharacterIterator(java.text.AttributedCharacterIterator) AbstractMap(java.util.AbstractMap) NumberFormat(com.ibm.icu.text.NumberFormat)

Example 7 with NumberFormat

use of com.ibm.icu.text.NumberFormat in project es6draft by anba.

the class PluralRulesObject method toFixedDecimal.

@SuppressWarnings("deprecation")
com.ibm.icu.text.PluralRules.FixedDecimal toFixedDecimal(double n) {
    NumberFormat nf = getNumberFormat();
    StringBuffer sb = new StringBuffer();
    FieldPosition fp = new FieldPosition(DecimalFormat.FRACTION_FIELD);
    nf.format(n, sb, fp);
    int v = fp.getEndIndex() - fp.getBeginIndex();
    long f = 0;
    if (v > 0) {
        ParsePosition pp = new ParsePosition(fp.getBeginIndex());
        f = nf.parse(sb.toString(), pp).longValue();
    }
    return new com.ibm.icu.text.PluralRules.FixedDecimal(n, v, f);
}
Also used : FieldPosition(java.text.FieldPosition) NumberFormat(com.ibm.icu.text.NumberFormat) ParsePosition(java.text.ParsePosition)

Aggregations

NumberFormat (com.ibm.icu.text.NumberFormat)7 CompactDecimalFormat (com.ibm.icu.text.CompactDecimalFormat)1 AttributedCharacterIterator (java.text.AttributedCharacterIterator)1 Attribute (java.text.AttributedCharacterIterator.Attribute)1 FieldPosition (java.text.FieldPosition)1 ParseException (java.text.ParseException)1 ParsePosition (java.text.ParsePosition)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 Image (org.eclipse.swt.graphics.Image)1 Point (org.eclipse.swt.graphics.Point)1