Here you can find the source of formatNumber(String format, double number)
Parameter | Description |
---|---|
number | the number to be formatted |
format | the format of the integer |
public static String formatNumber(String format, double number)
//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; } }