Java Number Format Pattern formatNumber(long number)

Here you can find the source of formatNumber(long number)

Description

format Number

License

Apache License

Declaration

public static String formatNumber(long number) 

Method Source Code

//package com.java2s;
/*/*w  w  w .  j  ava2s  .  co  m*/
 * Copyright 1999-2012 Alibaba Group.
 *  
 * 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 int ALIGN_RIGHT = 0;
    public static final int ALIGN_LEFT = 1;
    private static final char defaultSplitChar = ' ';
    private static final DecimalFormat numberFormat = new DecimalFormat("###,###");

    public static String formatNumber(long number) {
        return numberFormat.format(number);
    }

    /**
     * ????????????
     * 
     * @param s ????????????????????????
     * @param fillLength ?????
     * @return String
     */
    public static final String format(String s, int fillLength) {
        return format(s, fillLength, defaultSplitChar, ALIGN_LEFT);
    }

    /**
     * ????????????
     * 
     * @param i ????????????????????????????
     * @param fillLength ?????
     * @return String
     */
    public static final String format(int i, int fillLength) {
        return format(Integer.toString(i), fillLength, defaultSplitChar, ALIGN_RIGHT);
    }

    /**
     * ????????????
     * 
     * @param l ????????????????????????????
     * @param fillLength ?????
     * @return String
     */
    public static final String format(long l, int fillLength) {
        return format(Long.toString(l), fillLength, defaultSplitChar, ALIGN_RIGHT);
    }

    public static final String format(String s, int fillLength, char fillChar, int align) {
        if (s == null) {
            s = "";
        } else {
            s = s.trim();
        }
        int charLen = fillLength - s.length();
        if (charLen > 0) {
            char[] fills = new char[charLen];
            for (int i = 0; i < charLen; i++) {
                fills[i] = fillChar;
            }
            StringBuilder str = new StringBuilder(s);
            switch (align) {
            case ALIGN_RIGHT:
                str.insert(0, fills);
                break;
            case ALIGN_LEFT:
                str.append(fills);
                break;
            default:
                str.append(fills);
            }
            return str.toString();
        } else {
            return s;
        }
    }
}

Related

  1. formatLong(long value)
  2. formatNumber(final long number)
  3. formatNumber(final long number)
  4. formatNumber(int num)
  5. formatNumber(int source)
  6. formatNumber(long number)
  7. formatNumber(Number num)
  8. formatNumber(Number number)
  9. FormatNumber(Object o,String patter)