Search in sources :

Example 66 with DecimalFormat

use of java.text.DecimalFormat in project j2objc by google.

the class MessageFormat method toPattern.

/**
     * Returns a pattern representing the current state of the message format.
     * The string is constructed from internal information and therefore
     * does not necessarily equal the previously applied pattern.
     *
     * @return a pattern representing the current state of the message format
     */
public String toPattern() {
    // later, make this more extensible
    int lastOffset = 0;
    StringBuilder result = new StringBuilder();
    for (int i = 0; i <= maxOffset; ++i) {
        copyAndFixQuotes(pattern, lastOffset, offsets[i], result);
        lastOffset = offsets[i];
        result.append('{').append(argumentNumbers[i]);
        Format fmt = formats[i];
        if (fmt == null) {
        // do nothing, string format
        } else if (fmt instanceof NumberFormat) {
            if (fmt.equals(NumberFormat.getInstance(locale))) {
                result.append(",number");
            } else if (fmt.equals(NumberFormat.getCurrencyInstance(locale))) {
                result.append(",number,currency");
            } else if (fmt.equals(NumberFormat.getPercentInstance(locale))) {
                result.append(",number,percent");
            } else if (fmt.equals(NumberFormat.getIntegerInstance(locale))) {
                result.append(",number,integer");
            } else {
                if (fmt instanceof DecimalFormat) {
                    result.append(",number,").append(((DecimalFormat) fmt).toPattern());
                } else if (fmt instanceof ChoiceFormat) {
                    result.append(",choice,").append(((ChoiceFormat) fmt).toPattern());
                } else {
                // UNKNOWN
                }
            }
        } else if (fmt instanceof DateFormat) {
            int index;
            for (index = MODIFIER_DEFAULT; index < DATE_TIME_MODIFIERS.length; index++) {
                DateFormat df = DateFormat.getDateInstance(DATE_TIME_MODIFIERS[index], locale);
                if (fmt.equals(df)) {
                    result.append(",date");
                    break;
                }
                df = DateFormat.getTimeInstance(DATE_TIME_MODIFIERS[index], locale);
                if (fmt.equals(df)) {
                    result.append(",time");
                    break;
                }
            }
            if (index >= DATE_TIME_MODIFIERS.length) {
                if (fmt instanceof SimpleDateFormat) {
                    result.append(",date,").append(((SimpleDateFormat) fmt).toPattern());
                } else {
                // UNKNOWN
                }
            } else if (index != MODIFIER_DEFAULT) {
                result.append(',').append(DATE_TIME_MODIFIER_KEYWORDS[index]);
            }
        } else {
        //result.append(", unknown");
        }
        result.append('}');
    }
    copyAndFixQuotes(pattern, lastOffset, pattern.length(), result);
    return result.toString();
}
Also used : DecimalFormat(java.text.DecimalFormat) DecimalFormat(java.text.DecimalFormat)

Example 67 with DecimalFormat

use of java.text.DecimalFormat in project j2objc by google.

the class MessageFormat method makeFormat.

private void makeFormat(int position, int offsetNumber, StringBuilder[] textSegments) {
    String[] segments = new String[textSegments.length];
    for (int i = 0; i < textSegments.length; i++) {
        StringBuilder oneseg = textSegments[i];
        segments[i] = (oneseg != null) ? oneseg.toString() : "";
    }
    // get the argument number
    int argumentNumber;
    try {
        // always unlocalized!
        argumentNumber = Integer.parseInt(segments[SEG_INDEX]);
    } catch (NumberFormatException e) {
        throw new IllegalArgumentException("can't parse argument number: " + segments[SEG_INDEX], e);
    }
    if (argumentNumber < 0) {
        throw new IllegalArgumentException("negative argument number: " + argumentNumber);
    }
    // resize format information arrays if necessary
    if (offsetNumber >= formats.length) {
        int newLength = formats.length * 2;
        Format[] newFormats = new Format[newLength];
        int[] newOffsets = new int[newLength];
        int[] newArgumentNumbers = new int[newLength];
        System.arraycopy(formats, 0, newFormats, 0, maxOffset + 1);
        System.arraycopy(offsets, 0, newOffsets, 0, maxOffset + 1);
        System.arraycopy(argumentNumbers, 0, newArgumentNumbers, 0, maxOffset + 1);
        formats = newFormats;
        offsets = newOffsets;
        argumentNumbers = newArgumentNumbers;
    }
    int oldMaxOffset = maxOffset;
    maxOffset = offsetNumber;
    offsets[offsetNumber] = segments[SEG_RAW].length();
    argumentNumbers[offsetNumber] = argumentNumber;
    // now get the format
    Format newFormat = null;
    if (segments[SEG_TYPE].length() != 0) {
        int type = findKeyword(segments[SEG_TYPE], TYPE_KEYWORDS);
        switch(type) {
            case TYPE_NULL:
                // are treated as "{0}".
                break;
            case TYPE_NUMBER:
                switch(findKeyword(segments[SEG_MODIFIER], NUMBER_MODIFIER_KEYWORDS)) {
                    case MODIFIER_DEFAULT:
                        newFormat = NumberFormat.getInstance(locale);
                        break;
                    case MODIFIER_CURRENCY:
                        newFormat = NumberFormat.getCurrencyInstance(locale);
                        break;
                    case MODIFIER_PERCENT:
                        newFormat = NumberFormat.getPercentInstance(locale);
                        break;
                    case MODIFIER_INTEGER:
                        newFormat = NumberFormat.getIntegerInstance(locale);
                        break;
                    default:
                        // DecimalFormat pattern
                        try {
                            newFormat = new DecimalFormat(segments[SEG_MODIFIER], DecimalFormatSymbols.getInstance(locale));
                        } catch (IllegalArgumentException e) {
                            maxOffset = oldMaxOffset;
                            throw e;
                        }
                        break;
                }
                break;
            case TYPE_DATE:
            case TYPE_TIME:
                int mod = findKeyword(segments[SEG_MODIFIER], DATE_TIME_MODIFIER_KEYWORDS);
                if (mod >= 0 && mod < DATE_TIME_MODIFIER_KEYWORDS.length) {
                    if (type == TYPE_DATE) {
                        newFormat = DateFormat.getDateInstance(DATE_TIME_MODIFIERS[mod], locale);
                    } else {
                        newFormat = DateFormat.getTimeInstance(DATE_TIME_MODIFIERS[mod], locale);
                    }
                } else {
                    // SimpleDateFormat pattern
                    try {
                        newFormat = new SimpleDateFormat(segments[SEG_MODIFIER], locale);
                    } catch (IllegalArgumentException e) {
                        maxOffset = oldMaxOffset;
                        throw e;
                    }
                }
                break;
            case TYPE_CHOICE:
                try {
                    // ChoiceFormat pattern
                    newFormat = new ChoiceFormat(segments[SEG_MODIFIER]);
                } catch (Exception e) {
                    maxOffset = oldMaxOffset;
                    throw new IllegalArgumentException("Choice Pattern incorrect: " + segments[SEG_MODIFIER], e);
                }
                break;
            default:
                maxOffset = oldMaxOffset;
                throw new IllegalArgumentException("unknown format type: " + segments[SEG_TYPE]);
        }
    }
    formats[offsetNumber] = newFormat;
}
Also used : DecimalFormat(java.text.DecimalFormat) InvalidObjectException(java.io.InvalidObjectException) IOException(java.io.IOException) DecimalFormat(java.text.DecimalFormat)

Example 68 with DecimalFormat

use of java.text.DecimalFormat in project AndroidPicker by gzu-liyujiang.

the class ConvertUtils method toFileSizeString.

public static String toFileSizeString(long fileSize) {
    DecimalFormat df = new DecimalFormat("0.00");
    String fileSizeString;
    if (fileSize < KB) {
        fileSizeString = fileSize + "B";
    } else if (fileSize < MB) {
        fileSizeString = df.format((double) fileSize / KB) + "K";
    } else if (fileSize < GB) {
        fileSizeString = df.format((double) fileSize / MB) + "M";
    } else {
        fileSizeString = df.format((double) fileSize / GB) + "G";
    }
    return fileSizeString;
}
Also used : DecimalFormat(java.text.DecimalFormat)

Example 69 with DecimalFormat

use of java.text.DecimalFormat in project j2objc by google.

the class ElemNumber method getFormattedNumber.

// end formatNumberList method
/*
  * Get Formatted number
  */
/**
   * Format the given number and store it in the given buffer 
   *
   *
   * @param transformer non-null reference to the the current transform-time state.
   * @param contextNode The node that "." expresses.
   * @param numberType Type to format to
   * @param numberWidth Maximum length of formatted number
   * @param listElement Number to format
   * @param formattedNumber Buffer to store formatted number
   *
   * @throws javax.xml.transform.TransformerException
   */
private void getFormattedNumber(TransformerImpl transformer, int contextNode, char numberType, int numberWidth, long listElement, FastStringBuffer formattedNumber) throws javax.xml.transform.TransformerException {
    String letterVal = (m_lettervalue_avt != null) ? m_lettervalue_avt.evaluate(transformer.getXPathContext(), contextNode, this) : null;
    /**
     * Wrapper of Chars for converting integers into alpha counts.
     */
    CharArrayWrapper alphaCountTable = null;
    XResourceBundle thisBundle = null;
    switch(numberType) {
        case 'A':
            if (null == m_alphaCountTable) {
                thisBundle = (XResourceBundle) XResourceBundle.loadResourceBundle(org.apache.xml.utils.res.XResourceBundle.LANG_BUNDLE_NAME, getLocale(transformer, contextNode));
                m_alphaCountTable = (CharArrayWrapper) thisBundle.getObject(org.apache.xml.utils.res.XResourceBundle.LANG_ALPHABET);
            }
            int2alphaCount(listElement, m_alphaCountTable, formattedNumber);
            break;
        case 'a':
            if (null == m_alphaCountTable) {
                thisBundle = (XResourceBundle) XResourceBundle.loadResourceBundle(org.apache.xml.utils.res.XResourceBundle.LANG_BUNDLE_NAME, getLocale(transformer, contextNode));
                m_alphaCountTable = (CharArrayWrapper) thisBundle.getObject(org.apache.xml.utils.res.XResourceBundle.LANG_ALPHABET);
            }
            FastStringBuffer stringBuf = StringBufferPool.get();
            try {
                int2alphaCount(listElement, m_alphaCountTable, stringBuf);
                formattedNumber.append(stringBuf.toString().toLowerCase(getLocale(transformer, contextNode)));
            } finally {
                StringBufferPool.free(stringBuf);
            }
            break;
        case 'I':
            formattedNumber.append(long2roman(listElement, true));
            break;
        case 'i':
            formattedNumber.append(long2roman(listElement, true).toLowerCase(getLocale(transformer, contextNode)));
            break;
        case 0x3042:
            {
                thisBundle = (XResourceBundle) XResourceBundle.loadResourceBundle(org.apache.xml.utils.res.XResourceBundle.LANG_BUNDLE_NAME, new Locale("ja", "JP", "HA"));
                if (letterVal != null && letterVal.equals(Constants.ATTRVAL_TRADITIONAL))
                    formattedNumber.append(tradAlphaCount(listElement, thisBundle));
                else
                    //if (m_lettervalue_avt != null && m_lettervalue_avt.equals(Constants.ATTRVAL_ALPHABETIC))
                    formattedNumber.append(int2singlealphaCount(listElement, (CharArrayWrapper) thisBundle.getObject(org.apache.xml.utils.res.XResourceBundle.LANG_ALPHABET)));
                break;
            }
        case 0x3044:
            {
                thisBundle = (XResourceBundle) XResourceBundle.loadResourceBundle(org.apache.xml.utils.res.XResourceBundle.LANG_BUNDLE_NAME, new Locale("ja", "JP", "HI"));
                if ((letterVal != null) && letterVal.equals(Constants.ATTRVAL_TRADITIONAL))
                    formattedNumber.append(tradAlphaCount(listElement, thisBundle));
                else
                    //if (m_lettervalue_avt != null && m_lettervalue_avt.equals(Constants.ATTRVAL_ALPHABETIC))
                    formattedNumber.append(int2singlealphaCount(listElement, (CharArrayWrapper) thisBundle.getObject(org.apache.xml.utils.res.XResourceBundle.LANG_ALPHABET)));
                break;
            }
        case 0x30A2:
            {
                thisBundle = (XResourceBundle) XResourceBundle.loadResourceBundle(org.apache.xml.utils.res.XResourceBundle.LANG_BUNDLE_NAME, new Locale("ja", "JP", "A"));
                if (letterVal != null && letterVal.equals(Constants.ATTRVAL_TRADITIONAL))
                    formattedNumber.append(tradAlphaCount(listElement, thisBundle));
                else
                    //if (m_lettervalue_avt != null && m_lettervalue_avt.equals(Constants.ATTRVAL_ALPHABETIC))
                    formattedNumber.append(int2singlealphaCount(listElement, (CharArrayWrapper) thisBundle.getObject(org.apache.xml.utils.res.XResourceBundle.LANG_ALPHABET)));
                break;
            }
        case 0x30A4:
            {
                thisBundle = (XResourceBundle) XResourceBundle.loadResourceBundle(org.apache.xml.utils.res.XResourceBundle.LANG_BUNDLE_NAME, new Locale("ja", "JP", "I"));
                if (letterVal != null && letterVal.equals(Constants.ATTRVAL_TRADITIONAL))
                    formattedNumber.append(tradAlphaCount(listElement, thisBundle));
                else
                    //if (m_lettervalue_avt != null && m_lettervalue_avt.equals(Constants.ATTRVAL_ALPHABETIC))
                    formattedNumber.append(int2singlealphaCount(listElement, (CharArrayWrapper) thisBundle.getObject(org.apache.xml.utils.res.XResourceBundle.LANG_ALPHABET)));
                break;
            }
        case 0x4E00:
            {
                thisBundle = (XResourceBundle) XResourceBundle.loadResourceBundle(org.apache.xml.utils.res.XResourceBundle.LANG_BUNDLE_NAME, new Locale("zh", "CN"));
                if (letterVal != null && letterVal.equals(Constants.ATTRVAL_TRADITIONAL)) {
                    formattedNumber.append(tradAlphaCount(listElement, thisBundle));
                } else
                    //if (m_lettervalue_avt != null && m_lettervalue_avt.equals(Constants.ATTRVAL_ALPHABETIC))
                    int2alphaCount(listElement, (CharArrayWrapper) thisBundle.getObject(org.apache.xml.utils.res.XResourceBundle.LANG_ALPHABET), formattedNumber);
                break;
            }
        case 0x58F9:
            {
                thisBundle = (XResourceBundle) XResourceBundle.loadResourceBundle(org.apache.xml.utils.res.XResourceBundle.LANG_BUNDLE_NAME, new Locale("zh", "TW"));
                if (letterVal != null && letterVal.equals(Constants.ATTRVAL_TRADITIONAL))
                    formattedNumber.append(tradAlphaCount(listElement, thisBundle));
                else
                    //if (m_lettervalue_avt != null && m_lettervalue_avt.equals(Constants.ATTRVAL_ALPHABETIC))
                    int2alphaCount(listElement, (CharArrayWrapper) thisBundle.getObject(org.apache.xml.utils.res.XResourceBundle.LANG_ALPHABET), formattedNumber);
                break;
            }
        case 0x0E51:
            {
                thisBundle = (XResourceBundle) XResourceBundle.loadResourceBundle(org.apache.xml.utils.res.XResourceBundle.LANG_BUNDLE_NAME, new Locale("th", ""));
                if (letterVal != null && letterVal.equals(Constants.ATTRVAL_TRADITIONAL))
                    formattedNumber.append(tradAlphaCount(listElement, thisBundle));
                else
                    //if (m_lettervalue_avt != null && m_lettervalue_avt.equals(Constants.ATTRVAL_ALPHABETIC))
                    int2alphaCount(listElement, (CharArrayWrapper) thisBundle.getObject(org.apache.xml.utils.res.XResourceBundle.LANG_ALPHABET), formattedNumber);
                break;
            }
        case 0x05D0:
            {
                thisBundle = (XResourceBundle) XResourceBundle.loadResourceBundle(org.apache.xml.utils.res.XResourceBundle.LANG_BUNDLE_NAME, new Locale("he", ""));
                if (letterVal != null && letterVal.equals(Constants.ATTRVAL_TRADITIONAL))
                    formattedNumber.append(tradAlphaCount(listElement, thisBundle));
                else
                    //if (m_lettervalue_avt != null && m_lettervalue_avt.equals(Constants.ATTRVAL_ALPHABETIC))
                    int2alphaCount(listElement, (CharArrayWrapper) thisBundle.getObject(org.apache.xml.utils.res.XResourceBundle.LANG_ALPHABET), formattedNumber);
                break;
            }
        case 0x10D0:
            {
                thisBundle = (XResourceBundle) XResourceBundle.loadResourceBundle(org.apache.xml.utils.res.XResourceBundle.LANG_BUNDLE_NAME, new Locale("ka", ""));
                if (letterVal != null && letterVal.equals(Constants.ATTRVAL_TRADITIONAL))
                    formattedNumber.append(tradAlphaCount(listElement, thisBundle));
                else
                    //if (m_lettervalue_avt != null && m_lettervalue_avt.equals(Constants.ATTRVAL_ALPHABETIC))
                    int2alphaCount(listElement, (CharArrayWrapper) thisBundle.getObject(org.apache.xml.utils.res.XResourceBundle.LANG_ALPHABET), formattedNumber);
                break;
            }
        case 0x03B1:
            {
                thisBundle = (XResourceBundle) XResourceBundle.loadResourceBundle(org.apache.xml.utils.res.XResourceBundle.LANG_BUNDLE_NAME, new Locale("el", ""));
                if (letterVal != null && letterVal.equals(Constants.ATTRVAL_TRADITIONAL))
                    formattedNumber.append(tradAlphaCount(listElement, thisBundle));
                else
                    //if (m_lettervalue_avt != null && m_lettervalue_avt.equals(Constants.ATTRVAL_ALPHABETIC))
                    int2alphaCount(listElement, (CharArrayWrapper) thisBundle.getObject(org.apache.xml.utils.res.XResourceBundle.LANG_ALPHABET), formattedNumber);
                break;
            }
        case 0x0430:
            {
                thisBundle = (XResourceBundle) XResourceBundle.loadResourceBundle(org.apache.xml.utils.res.XResourceBundle.LANG_BUNDLE_NAME, new Locale("cy", ""));
                if (letterVal != null && letterVal.equals(Constants.ATTRVAL_TRADITIONAL))
                    formattedNumber.append(tradAlphaCount(listElement, thisBundle));
                else
                    //if (m_lettervalue_avt != null && m_lettervalue_avt.equals(Constants.ATTRVAL_ALPHABETIC))
                    int2alphaCount(listElement, (CharArrayWrapper) thisBundle.getObject(org.apache.xml.utils.res.XResourceBundle.LANG_ALPHABET), formattedNumber);
                break;
            }
        default:
            // "1"
            DecimalFormat formatter = getNumberFormatter(transformer, contextNode);
            String padString = formatter == null ? String.valueOf(0) : formatter.format(0);
            String numString = formatter == null ? String.valueOf(listElement) : formatter.format(listElement);
            int nPadding = numberWidth - numString.length();
            for (int k = 0; k < nPadding; k++) {
                formattedNumber.append(padString);
            }
            formattedNumber.append(numString);
    }
}
Also used : Locale(java.util.Locale) FastStringBuffer(org.apache.xml.utils.FastStringBuffer) CharArrayWrapper(org.apache.xml.utils.res.CharArrayWrapper) DecimalFormat(java.text.DecimalFormat) XResourceBundle(org.apache.xml.utils.res.XResourceBundle)

Example 70 with DecimalFormat

use of java.text.DecimalFormat in project j2objc by google.

the class ElemNumber method getNumberFormatter.

/**
   * Get the number formatter to be used the format the numbers
   *
   * @param transformer non-null reference to the the current transform-time state.
   * @param contextNode The node that "." expresses.
   *
   * ($objectName$) @return The number formatter to be used
   *
   * @throws TransformerException
   */
private DecimalFormat getNumberFormatter(TransformerImpl transformer, int contextNode) throws TransformerException {
    // Patch from Steven Serocki
    // Maybe we really want to do the clone in getLocale() and return  
    // a clone of the default Locale??
    Locale locale = (Locale) getLocale(transformer, contextNode).clone();
    // Helper to format local specific numbers to strings.
    DecimalFormat formatter = null;
    //synchronized (locale)
    //{
    //     formatter = (DecimalFormat) NumberFormat.getNumberInstance(locale);
    //}
    String digitGroupSepValue = (null != m_groupingSeparator_avt) ? m_groupingSeparator_avt.evaluate(transformer.getXPathContext(), contextNode, this) : null;
    // validated statically in XSLTAttributeDef.java.
    if ((digitGroupSepValue != null) && (!m_groupingSeparator_avt.isSimple()) && (digitGroupSepValue.length() != 1)) {
        transformer.getMsgMgr().warn(this, XSLTErrorResources.WG_ILLEGAL_ATTRIBUTE_VALUE, new Object[] { Constants.ATTRNAME_NAME, m_groupingSeparator_avt.getName() });
    }
    String nDigitsPerGroupValue = (null != m_groupingSize_avt) ? m_groupingSize_avt.evaluate(transformer.getXPathContext(), contextNode, this) : null;
    // TODO: Handle digit-group attributes
    if ((null != digitGroupSepValue) && (null != nDigitsPerGroupValue) && // Ignore if separation value is empty string
    (digitGroupSepValue.length() > 0)) {
        try {
            formatter = (DecimalFormat) NumberFormat.getNumberInstance(locale);
            formatter.setGroupingSize(Integer.valueOf(nDigitsPerGroupValue).intValue());
            DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();
            symbols.setGroupingSeparator(digitGroupSepValue.charAt(0));
            formatter.setDecimalFormatSymbols(symbols);
            formatter.setGroupingUsed(true);
        } catch (NumberFormatException ex) {
            formatter.setGroupingUsed(false);
        }
    }
    return formatter;
}
Also used : Locale(java.util.Locale) DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat)

Aggregations

DecimalFormat (java.text.DecimalFormat)823 DecimalFormatSymbols (java.text.DecimalFormatSymbols)94 NumberFormat (java.text.NumberFormat)93 IOException (java.io.IOException)48 BigDecimal (java.math.BigDecimal)47 ArrayList (java.util.ArrayList)44 IFormatterTextCallBack (org.xclcharts.common.IFormatterTextCallBack)33 ParseException (java.text.ParseException)31 Test (org.junit.Test)31 File (java.io.File)29 IFormatterDoubleCallBack (org.xclcharts.common.IFormatterDoubleCallBack)29 Support_DecimalFormat (tests.support.Support_DecimalFormat)28 Date (java.util.Date)25 SimpleDateFormat (java.text.SimpleDateFormat)24 HashMap (java.util.HashMap)24 Locale (java.util.Locale)22 PrintWriter (java.io.PrintWriter)20 UsageVO (com.cloud.usage.UsageVO)19 List (java.util.List)18 JFreeChart (org.jfree.chart.JFreeChart)18