Java BigDecimal Format format(BigDecimal decData, int precision, int scale)

Here you can find the source of format(BigDecimal decData, int precision, int scale)

Description

Format.

License

Open Source License

Parameter

Parameter Description
decData the dec data
precision the precision
scale the scale

Exception

Parameter Description
Exception the exception

Return

the string

Declaration

public static String format(BigDecimal decData, int precision, int scale) throws Exception 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright  (c) 2015-2016, WSO2.Telco Inc. (http://www.wso2telco.com) All Rights Reserved.
 * //  w ww  .ja va2s  .co  m
 *  WSO2.Telco Inc. licences this file to you 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;

public class Main {
    /**
     * Format.
     *
     * @param strData the str data
     * @param finalLen the final len
     * @return the string
     * @throws Exception the exception
     */
    public static String format(String strData, int finalLen) throws Exception {
        String finalStr;
        if (finalLen <= strData.length()) {
            finalStr = strData.substring(0, finalLen);
        } else {
            finalStr = strData;
            for (int i = strData.length(); i < finalLen; i++) {
                finalStr = finalStr + " ";
            }
        }
        return (finalStr);
    }

    /**
     * Format.
     *
     * @param intData the int data
     * @param finalLen the final len
     * @return the string
     * @throws Exception the exception
     */
    public static String format(int intData, int finalLen) throws Exception {
        String strData = String.valueOf(intData);
        String finalStr;
        if (finalLen <= strData.length()) {
            finalStr = strData.substring(0, finalLen);
        } else {
            finalStr = "";
            for (int i = 0; i < finalLen - strData.length(); i++) {
                finalStr = finalStr + " ";
            }
            finalStr = finalStr + strData;
        }
        return (finalStr);
    }

    /**
     * Format.
     *
     * @param integerData the integer data
     * @param finalLen the final len
     * @return the string
     * @throws Exception the exception
     */
    public static String format(Integer integerData, int finalLen) throws Exception {
        int intData;
        String finalStr;

        intData = integerData.intValue();
        finalStr = format(intData, finalLen);

        return (finalStr);
    }

    /**
     * Format.
     *
     * @param doubData the doub data
     * @param precision the precision
     * @param scale the scale
     * @return the string
     * @throws Exception the exception
     */
    public static String format(double doubData, int precision, int scale) throws Exception {
        BigDecimal decData = new BigDecimal(doubData);
        decData = decData.setScale(scale, BigDecimal.ROUND_HALF_EVEN);
        String strData = decData.toString();

        // prepare the final string
        int finalLen = precision + 1;
        String finalStr;
        if (finalLen <= strData.length()) {
            finalStr = strData.substring(0, finalLen);
        } else {
            finalStr = "";
            for (int i = 0; i < finalLen - strData.length(); i++) {
                finalStr = finalStr + " ";
            }
            finalStr = finalStr + strData;
        }

        return (finalStr);
    }

    /**
     * Format.
     *
     * @param decData the dec data
     * @param precision the precision
     * @param scale the scale
     * @return the string
     * @throws Exception the exception
     */
    public static String format(BigDecimal decData, int precision, int scale) throws Exception {
        decData = decData.setScale(scale, BigDecimal.ROUND_HALF_EVEN);
        String strData = decData.toString();

        // prepare the final string
        int finalLen = precision + 1;
        String finalStr;
        if (finalLen <= strData.length()) {
            finalStr = strData.substring(0, finalLen);
        } else {
            finalStr = "";
            for (int i = 0; i < finalLen - strData.length(); i++) {
                finalStr = finalStr + " ";
            }
            finalStr = finalStr + strData;
        }

        return (finalStr);
    }

    /**
     * Format.
     *
     * @param doubleData the double data
     * @param precision the precision
     * @param scale the scale
     * @return the string
     * @throws Exception the exception
     */
    public static String format(Double doubleData, int precision, int scale) throws Exception {
        double doubData;

        doubData = doubleData.doubleValue();
        return (format(doubData, precision, scale));
    }
}

Related

  1. fmtBdToInt(BigDecimal bd)
  2. format(BigDecimal amount, String format)
  3. format(BigDecimal decimal)
  4. format(BigDecimal n, int prec)
  5. format(BigDecimal n, int prec)
  6. format(BigDecimal no, String formatter)