Java Fraction Format formatNumber(String format, double number)

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

Description

Converts the number into the specified format

License

Apache License

Parameter

Parameter Description
number the number to be formatted
format the format of the integer

Return

the number in the specified format

Declaration

public static String formatNumber(String format, double number) 

Method Source Code

//package com.java2s;
/*/*w  w w .j  ava  2 s  . c  om*/
 * 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. formatNumber(final double number)
  2. formatNumber(Float d, int scalar)
  3. formatNumber(float num, String format)
  4. formatNumber(int fractionDigits, double value)
  5. formatNumber(String Format, double Num)
  6. formatNumber(String number, int maxFractionalDigits)
  7. formatNumberAttribute(double v)
  8. formatNumFraction(float num, int unit)
  9. formatNumValueWithComma(double val)