Java BigDecimal Format format(BigDecimal number, String format)

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

Description

Method that formats the given BigDecimal into a String with the specified format

License

Open Source License

Parameter

Parameter Description
number the BigDecimal to format
format the format to use

Return

the resultant String

Declaration

public static String format(BigDecimal number, String format) 

Method Source Code


//package com.java2s;
/*//from   w  ww .jav a 2 s.c o m
Copyright 2012 Juan Mart?n Runge
    
jmrunge@gmail.com
http://www.zirsi.com.ar
    
This file is part of ZirUtils.
    
ZirUtils is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
ZirUtils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with ZirUtils.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class Main {
    /**
     * Method that formats the given BigDecimal into a String with the specified format
     * 
     * @param number the BigDecimal to format
     * @param format the format to use
     * @return the resultant String
     * @see DecimalFormat#format(double) 
     */
    public static String format(BigDecimal number, String format) {
        if (number == null)
            return null;
        DecimalFormat snf = new DecimalFormat(format);
        return snf.format(number.doubleValue());
    }

    /**
     * Method that formats the given int into a String with the specified format
     * 
     * @param number the int to format
     * @param format the format to use
     * @return the resultant String
     * @see format(long, String)
     */
    public static String format(int number, String format) {
        return format(new Long(number), format);
    }

    /**
     * Method that formats the given long into a String with the specified format
     * 
     * @param number the long to format
     * @param format the format to use
     * @return the resultant String
     * @see DecimalFormat#format(long) 
     */
    public static String format(long number, String format) {
        DecimalFormat snf = new DecimalFormat(format);
        return snf.format(number);
    }
}

Related

  1. format(BigDecimal n, int prec)
  2. format(BigDecimal no, String formatter)
  3. format(BigDecimal num)
  4. format(BigDecimal num)
  5. format(BigDecimal num)
  6. format(final BigDecimal bd)
  7. format(Object value, Integer precision)
  8. format(String pattern, final double secondInDouble)
  9. format2Scale(BigDecimal obj)