Package com.ibm.icu.text

Examples of com.ibm.icu.text.NumberFormat


     * @param style
     * @draft ICU 3.6
     * @provisional This API might change or be removed in a future release.
     */
    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;
View Full Code Here


     * hour is set to fields[1], minute is set to fields[2] and second is
     * set to fields[3].
     * @return Returns true when the given custom id is valid.
     */
    static boolean parseCustomID(String id, int[] fields) {
        NumberFormat numberFormat = null;
        String idUppercase = id.toUpperCase();

        if (id != null && id.length() > kGMT_ID.length() &&
            idUppercase.startsWith(kGMT_ID)) {
            ParsePosition pos = new ParsePosition(kGMT_ID.length());
            int sign = 1;
            int hour = 0;
            int min = 0;
            int sec = 0;

            if (id.charAt(pos.getIndex()) == 0x002D /*'-'*/) {
                sign = -1;
            } else if (id.charAt(pos.getIndex()) != 0x002B /*'+'*/) {
                return false;
            }
            pos.setIndex(pos.getIndex() + 1);

            numberFormat = NumberFormat.getInstance();
            numberFormat.setParseIntegerOnly(true);

            // Look for either hh:mm, hhmm, or hh
            int start = pos.getIndex();

            Number n = numberFormat.parse(id, pos);
            if (pos.getIndex() == start) {
                return false;
            }
            hour = n.intValue();

            if (pos.getIndex() < id.length()){
                if (pos.getIndex() - start > 2
                        || id.charAt(pos.getIndex()) != 0x003A /*':'*/) {
                    return false;
                }
                // hh:mm
                pos.setIndex(pos.getIndex() + 1);
                int oldPos = pos.getIndex();
                n = numberFormat.parse(id, pos);
                if ((pos.getIndex() - oldPos) != 2) {
                    // must be 2 digits
                    return false;
                }
                min = n.intValue();
                if (pos.getIndex() < id.length()) {
                    if (id.charAt(pos.getIndex()) != 0x003A /*':'*/) {
                        return false;
                    }
                    // [:ss]
                    pos.setIndex(pos.getIndex() + 1);
                    oldPos = pos.getIndex();
                    n = numberFormat.parse(id, pos);
                    if (pos.getIndex() != id.length()
                            || (pos.getIndex() - oldPos) != 2) {
                        return false;
                    }
                    sec = n.intValue();
View Full Code Here

     * hour is set to fields[1], minute is set to fields[2] and second is
     * set to fields[3].
     * @return Returns true when the given custom id is valid.
     */
    static boolean parseCustomID(String id, int[] fields) {
        NumberFormat numberFormat = null;
        String idUppercase = id.toUpperCase();

        if (id != null && id.length() > kGMT_ID.length() &&
            idUppercase.startsWith(kGMT_ID)) {
            ParsePosition pos = new ParsePosition(kGMT_ID.length());
            int sign = 1;
            int hour = 0;
            int min = 0;
            int sec = 0;

            if (id.charAt(pos.getIndex()) == 0x002D /*'-'*/) {
                sign = -1;
            } else if (id.charAt(pos.getIndex()) != 0x002B /*'+'*/) {
                return false;
            }
            pos.setIndex(pos.getIndex() + 1);

            numberFormat = NumberFormat.getInstance();
            numberFormat.setParseIntegerOnly(true);

            // Look for either hh:mm, hhmm, or hh
            int start = pos.getIndex();

            Number n = numberFormat.parse(id, pos);
            if (pos.getIndex() == start) {
                return false;
            }
            hour = n.intValue();

            if (pos.getIndex() < id.length()){
                if (pos.getIndex() - start > 2
                        || id.charAt(pos.getIndex()) != 0x003A /*':'*/) {
                    return false;
                }
                // hh:mm
                pos.setIndex(pos.getIndex() + 1);
                int oldPos = pos.getIndex();
                n = numberFormat.parse(id, pos);
                if ((pos.getIndex() - oldPos) != 2) {
                    // must be 2 digits
                    return false;
                }
                min = n.intValue();
                if (pos.getIndex() < id.length()) {
                    if (id.charAt(pos.getIndex()) != 0x003A /*':'*/) {
                        return false;
                    }
                    // [:ss]
                    pos.setIndex(pos.getIndex() + 1);
                    oldPos = pos.getIndex();
                    n = numberFormat.parse(id, pos);
                    if (pos.getIndex() != id.length()
                            || (pos.getIndex() - oldPos) != 2) {
                        return false;
                    }
                    sec = n.intValue();
View Full Code Here

     */
    public NumberFormat getNumberFormat(int style) {
        if (style < 0 || style >= NF_LIMIT) {
            throw new IllegalArgumentException("Illegal number format type");
        }
        NumberFormat result = null;
        if (numberFormats != null) {
            result = numberFormats[style];
        }
        if (result != null) {
            result = (NumberFormat) result.clone(); // clone for safety (later optimize)
        } else {
            result = guessNumberFormat(style);
        }
        return result;
    }
View Full Code Here

     * @param style
     * @draft ICU 3.6
     * @provisional This API might change or be removed in a future release.
     */
    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;
View Full Code Here

  private static Map getConverterMap() {
    // using string-based lookup avoids loading of too many classes
    if (converterMap == null) {
      //NumberFormat to be shared across converters for the formatting of integer values
      NumberFormat integerFormat = NumberFormat.getIntegerInstance();
      //NumberFormat to be shared across converters for formatting non integer values
      NumberFormat numberFormat = NumberFormat.getNumberInstance();
     
      converterMap = new HashMap();
      converterMap
          .put(
              new Pair("java.util.Date", "java.lang.String"), "org.eclipse.core.internal.databinding.conversion.DateToStringConverter"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
View Full Code Here

    image = image.trim();
    if (!image.isEmpty()) {
      String toParse = image;
      if (toParse.length() > 1 && toParse.charAt(0) == '+')
        toParse = toParse.substring(1);
      NumberFormat nf = NumberFormat.getNumberInstance(iLocale);
      ParsePosition p = new ParsePosition(0);
      Number ret = nf.parse(toParse, p);
      int errIndex = p.getErrorIndex();
      //System.out.println("Ind=" + index + " ErrInd=" + errIndex);
      if (errIndex == -1) {
        if (p.getIndex() >= toParse.length() || lenient)
          return ret;
View Full Code Here

    lno.setDefault();
    return formatNumber(n, lno);
  }

  public String formatNumber(final Number n, final LotusNumberOptions lno) {
    NumberFormat nf;
    /*
     * It would have been more convenient to use NumberFormat.getInstance(locale, style),
     * but this method is private in com.ibm.icu_3.8.1.v20120530.jar.
     * (Seems to be public as of ICU 4.2.)
     */
    if (lno.format == 'C')
      nf = NumberFormat.getCurrencyInstance(iLocale);
    else if (lno.format == 'S')
      nf = NumberFormat.getScientificInstance(iLocale);
    else if (lno.format == '%')
      nf = NumberFormat.getPercentInstance(iLocale);
    else
      nf = NumberFormat.getNumberInstance(iLocale);
    nf.setGroupingUsed(lno.useGrouping);
    nf.setMaximumIntegerDigits(1000);
    if (lno.fractionDigits != -1) {
      nf.setMinimumFractionDigits(lno.fractionDigits);
      nf.setMaximumFractionDigits(lno.fractionDigits);
    } else
      nf.setMaximumFractionDigits(1000);
    String ret = nf.format(n);
    do {
      if (lno.format != 'G' || ret.length() <= 15)
        break;
      /*
       * In this case, Lotus implicitly switches to scientific style.
       * When useGrouping is in effect, the limit decreases from 15 to 12 in Lotus
       * (i.e. the grouping bytes are likewise counted), but we are not going to
       *  imitate this strange behaviour.
       */
      String tester = ret;
      if (lno.useGrouping) {
        nf.setGroupingUsed(false);
        tester = nf.format(n);
      }
      int minus = (tester.charAt(0) == '-') ? 1 : 0;
      int lh = tester.length();
      if (lh - minus <= 15)
        break;
      int komma = minus;
      for (; komma < lh; komma++)
        if (!Character.isDigit(tester.charAt(komma)))
          break;
      if (komma - minus <= 15)
        break;
      nf = NumberFormat.getScientificInstance(iLocale);
      nf.setGroupingUsed(lno.useGrouping);
      ret = nf.format(n);
    } while (false);
    if (lno.negativeAsParentheses && ret.charAt(0) == '-')
      ret = '(' + ret.substring(1) + ')';
    return ret;
  }
View Full Code Here

    @Override
    public String formatNumber(int number, Locale locale) {
        if (locale == null) {
            locale = Aura.getLocalizationAdapter().getAuraLocale().getNumberLocale();
        }
        NumberFormat nf = NumberFormat.getNumberInstance(locale);
        return nf.format(number);
    }
View Full Code Here

    @Override
    public String formatNumber(long number, Locale locale) {
        if (locale == null) {
            locale = Aura.getLocalizationAdapter().getAuraLocale().getNumberLocale();
        }
        NumberFormat nf = NumberFormat.getNumberInstance(locale);
        return nf.format(number);
    }
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.