Java Number Format formatNumber(Object num)

Here you can find the source of formatNumber(Object num)

Description

format Number

License

Open Source License

Declaration

public static String formatNumber(Object num) 

Method Source Code

//package com.java2s;
/*//  w w w  .ja v a 2 s  . co  m
 * $RCSfile: LogStringUtil,v $$
 * $Revision: 1.0  $
 * $Date: 2010-12-09  $
 *
 * Copyright (C) 2011 GyTech, Inc. All rights reserved.
 *
 * This software is the proprietary information of GyTech, Inc.
 * Use is subject to license terms.
 */

public class Main {
    public static String formatNumber(Object num) {
        if (num == null)
            return null;

        String number = num.toString().trim();

        if (number.length() == 0)
            return null;

        if (number.indexOf(".") == -1)
            return number;

        return number.length() - number.indexOf(".") > 2 ? number.substring(0, number.indexOf(".") + 3) : number;
    }

    /**
     * <p>Description:Trim the input string</p>
     * @param input string
     * @return the trimmed string
     */
    public static String trim(String str) {
        return (str == null ? "" : str.trim());
    }

    /**
    * <p>Description:Sub a string with the length, </p>
    * @param srcStr source string
    * @param subLen the length what you want to sub a string
    * @return subLen the length of sub string
    */
    public static String subString(String srcStr, int subLen) {
        byte[] bytes = srcStr.getBytes();
        //add by yujie
        //when logwarn system will remove some character of chinese string
        //on 2005/10/18
        if (bytes.length <= subLen)
            return srcStr;

        char[] chars = srcStr.toCharArray();
        StringBuffer sb = new StringBuffer();

        for (int i = 0, j = 0; i < subLen && i < bytes.length && j < chars.length;) {
            char cbyte = (char) bytes[i];
            //normal English character
            if (cbyte == chars[j]) {
                sb.append(chars[j]);
                i++;
                j++;
            } else {//Chinese character
                    //if adding next Chinese character will be over limitted, 
                    //then will not add this character
                if (i + 1 < subLen) {
                    sb.append(chars[j]);
                    //one chinese character will be equal two bytes
                    i++;
                    i++;
                    j++;
                } else
                    break;
            }
        }

        return sb.toString();
    }
}

Related

  1. formatNumber(long number)
  2. formatNumber(long number, int digits)
  3. formatNumber(long pNum, int pLen)
  4. formatNumber(long value)
  5. formatNumber(Number number)
  6. formatNumber(String formatStr, int length, boolean leftPadding, char paddingCharacter)
  7. formatNumber(String num, int n)
  8. formatNumber(String s, Character thousandsSeparator, Character decimalSeparator)
  9. formatNumber(String value)