Android String Format formattedNumber(String number)

Here you can find the source of formattedNumber(String number)

Description

formatted Number

License

Apache License

Declaration

public static String formattedNumber(String number) 

Method Source Code

//package com.java2s;
/**//ww  w .  ja  v a  2 s .  c om
 * Copyright 2013 ??????
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.DecimalFormat;

public class Main {
    public static final String EMPTY = "";

    public static String formattedNumber(String number) {
        if (number == null)
            return "0";

        String fn = "0";
        if (isNumber(number)) {
            DecimalFormat df = new DecimalFormat("#,##0");
            fn = df.format(Double.parseDouble(number));
        }

        return fn;
    }

    public static boolean isNumber(String str) {
        try {
            Integer.valueOf(str);
        } catch (Exception e) {
            return false;
        }

        return true;
    }

    public static String format(String str, String obj) {
        if (str == null || obj == null)
            return str;

        return replace(str, "{0}", obj);
    }

    public static String format(String str, String[] obj) {
        if (str == null || obj == null)
            return str;

        for (int i = 0; i < obj.length; i++)
            str = replace(str, "{" + i + "}", obj[i]);

        return str;
    }

    public static String format(String str, Object... obj) {
        if (str == null || obj == null)
            return str;

        for (int i = 0; i < obj.length; i++)
            str = replace(str, "{" + i + "}", String.valueOf(obj[i]));

        return str;
    }

    public static String replace(String s, char oldSub, char newSub) {
        return replace(s, oldSub, Character.valueOf(newSub));
    }

    public static String replace(String s, char oldSub, String newSub) {
        if ((s == null) || (newSub == null)) {
            return null;
        }

        char[] c = s.toCharArray();

        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < c.length; i++) {
            if (c[i] == oldSub) {
                sb.append(newSub);
            } else {
                sb.append(c[i]);
            }
        }

        return sb.toString();
    }

    public static String replace(String s, String oldSub, String newSub) {
        if ((s == null) || (oldSub == null) || (newSub == null)) {
            return null;
        }

        int y = s.indexOf(oldSub);

        if (y >= 0) {
            StringBuffer sb = new StringBuffer();
            int length = oldSub.length();
            int x = 0;

            while (x <= y) {
                sb.append(s.substring(x, y));
                sb.append(newSub);
                x = y + length;
                y = s.indexOf(oldSub, x);
            }

            sb.append(s.substring(x));

            return sb.toString();
        } else {
            return s;
        }
    }

    public static String replace(String s, String[] oldSubs,
            String[] newSubs) {
        if ((s == null) || (oldSubs == null) || (newSubs == null)) {
            return null;
        }

        if (oldSubs.length != newSubs.length) {
            return s;
        }

        for (int i = 0; i < oldSubs.length; i++) {
            s = replace(s, oldSubs[i], newSubs[i]);
        }

        return s;
    }

    public static String replace(String s, String[] oldSubs, char[] newSubs) {
        if ((s == null) || (oldSubs == null) || (newSubs == null)) {
            return null;
        }

        if (oldSubs.length != newSubs.length) {
            return s;
        }

        for (int i = 0; i < oldSubs.length; i++) {
            s = replace(s, oldSubs[i], String.valueOf(newSubs[i]));
        }
        return s;
    }

    public static String substring(String str, int beginIndex) {
        if (str == null) {
            return null;
        }

        if (beginIndex < 0) {
            return EMPTY;
        }

        if (beginIndex > str.length()) {
            return EMPTY;
        }

        return str.substring(beginIndex);
    }

    public static String substring(String str, int beginIndex, int endIndex) {
        if (str == null) {
            return null;
        }

        if (beginIndex < 0 || endIndex < 0) {
            return EMPTY;
        }

        if (endIndex > str.length()) {
            endIndex = str.length();
        }

        if (beginIndex > endIndex || beginIndex > str.length()) {
            return EMPTY;
        }

        return str.substring(beginIndex, endIndex);
    }
}

Related

  1. format(String str, Object... obj)
  2. format(String str, Object... args)
  3. simpleFormat(String s, Object aobj[])
  4. format(String str, Object... args)
  5. formatAccountNo(String accountNo)
  6. currentTime(CharSequence inFormat)
  7. formatIndent(String whiteSpace)
  8. formatNumberWithDecimals(Locale locale, String stringToFormat, int exactNumberOfDecimals)
  9. formatNumberWithDecimals(Locale locale, String stringToFormat, int minimalNumberOfDecimals, int maximumNumberOfDecimals)