Java BigDecimal Format formatBigDecimal(BigDecimal n, int dp)

Here you can find the source of formatBigDecimal(BigDecimal n, int dp)

Description

Sets [n] to [dp] decimal places.

License

Open Source License

Parameter

Parameter Description
n the BigDecimal to format
dp the wanted number of decimal places

Return

the formated BigDecimal

Declaration

public static BigDecimal formatBigDecimal(BigDecimal n, int dp) 

Method Source Code


//package com.java2s;
/*/* www. j  a v a 2  s. c  om*/
 * BigDecimalHelper.java
 * Copyright 2003 (C) Jonas Karlsson <jujutsunerd@sf.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Created on April 12, 2003, 3:20 AM
 */

import java.math.BigDecimal;

public class Main {
    /**
     * Sets [n] to [dp] decimal places.
     * @param n the BigDecimal to format
     * @param dp the wanted number of decimal places
     * @return the formated BigDecimal
     */
    public static BigDecimal formatBigDecimal(BigDecimal n, int dp) {
        return n.setScale(dp, BigDecimal.ROUND_HALF_UP); // Sets scale and rounds up if most significant (cut off) number >= 5
    }
}

Related

  1. formatBetrag(BigDecimal bdBetrag, Locale locale)
  2. formatBigDecimal(BigDecimal bd, NumberFormat format)
  3. formatBigDecimal(BigDecimal bigDecimal)
  4. formatBigDecimal(BigDecimal bigDecimal, int scale)
  5. formatBigDecimal(BigDecimal d)
  6. formatBigDecimal(BigDecimal number, int val)
  7. formatBigDecimal(BigDecimal valor, int decimal)
  8. formatBigDecimal(BigDecimal value, char thousandSep, char decimalPoint, int numDecimals)
  9. formatBigDecimal(String value)