Java BigDecimal Format formatNumber(String format, BigDecimal number)

Here you can find the source of formatNumber(String format, BigDecimal number)

Description

format Number

License

Apache License

Declaration

public static String formatNumber(String format, BigDecimal number) 

Method Source Code

//package com.java2s;
/*//  w  ww  .j  a v  a 2 s  . c  o m
 * Copyright 2004 Senunkan Shinryuu
 *
 * 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.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;

public class Main {
    /**
     * Converts the number into the specified format
     * @param number the number to be formatted
     * @param format the format of the integer
     * @return the number in the specified format
     */
    public static String formatNumber(String format, long number) {
        if (format == null)
            return "" + number;
        DecimalFormat df = new DecimalFormat(format);
        String retVal = null;
        try {
            retVal = df.format(number);
        } catch (Exception e) {
            retVal = "" + number;
        }
        return retVal;
    }

    /**
     * @see NumeralUtil#formatNumber(long, String)
     */
    public static String formatNumber(String format, int number) {
        return formatNumber(format, (long) number);
    }

    /**
     * @see NumeralUtil#formatNumber(long, String)
     */
    public static String formatNumber(String format, short number) {
        return formatNumber(format, (long) number);
    }

    /**
     * @see NumeralUtil#formatNumber(long, String)
     */
    public static String formatNumber(String format, byte number) {
        return formatNumber(format, (long) number);
    }

    /**
     * Converts the number into the specified format
     * @param number the number to be formatted
     * @param format the format of the integer
     * @return the number in the specified format
     */
    public static String formatNumber(String format, double number) {
        if (format == null)
            return "" + number;
        DecimalFormat df = new DecimalFormat(format);
        String retVal = null;
        try {
            retVal = df.format(number);
        } catch (Exception e) {
            retVal = "" + number;
        }
        return retVal;
    }

    /**
     * @see ConvertUtil#formatNumber(double, String)
     */
    public static String formatNumber(String format, float number) {
        return formatNumber(format, (double) number);
    }

    /**
     * Converts the number into the specified format
     * @param number the number to be formatted
     * @param format the format of the integer
     * @return the number in the specified format
     */
    public static String formatNumber(String format, BigInteger number) {
        if (format == null)
            return "" + number;
        DecimalFormat df = new DecimalFormat(format);
        String retVal = null;
        try {
            retVal = df.format(number);
        } catch (Exception e) {
            retVal = "" + number;
        }
        return retVal;
    }

    /**
     * @see ConvertUtil#formatNumber(double, String)
     */
    public static String formatNumber(String format, BigDecimal number) {
        if (format == null)
            return "" + number;
        DecimalFormat df = new DecimalFormat(format);
        String retVal = null;
        try {
            retVal = df.format(number);
        } catch (Exception e) {
            retVal = "" + number;
        }
        return retVal;
    }
}

Related

  1. formatManey(BigDecimal date)
  2. formatMoney(BigDecimal money, int scale, double divisor)
  3. formatMoney(long value)
  4. formatNoDecimalPoint(double amt)
  5. formatNumber(double number)
  6. formatNumberByMaxFrac(BigDecimal number, int val)
  7. formatNumberByMaxFracUseGrp(BigDecimal number, int val)
  8. formatNumberCroreFormat(final BigDecimal num)
  9. formatNumberDouble(Double amount, int scale)