Java BigDecimal Parse toDecimalString(String str, int decimal)

Here you can find the source of toDecimalString(String str, int decimal)

Description

to Decimal String

License

Open Source License

Declaration

public static String toDecimalString(String str, int decimal) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.DecimalFormat;

public class Main {
    public static String toDecimalString(String str, int decimal) {
        if (null == str) {
            return "";
        }/*from  w  ww  .j  av  a 2s . co  m*/
        return toDecimalString(Double.valueOf(str.trim()), decimal);
    }

    public static String toDecimalString(Double num, int decimal) {
        if (num == null) {
            return "";
        }
        String str = num.toString();
        String formatStr = "#.";
        for (int i = 0; i < decimal; ++i) {
            formatStr = formatStr + "0";
        }
        DecimalFormat df = new DecimalFormat(formatStr);
        try {
            str = df.format(num);
        } catch (Exception localException) {
        }
        return str;
    }
}

Related

  1. parseTo_BigDecimal(String s)
  2. parseToBigDecimal(Object value)
  3. string2BigDecimal(String valor)
  4. stringToBigDecimal(String numeroStr)
  5. strToFloat(String text, int decimals)