Converting String into BigDecimal - Java java.math

Java examples for java.math:BigDecimal Convert

Description

Converting String into BigDecimal

Demo Code


//package com.java2s;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;

public class Main {


    /**//from w w  w . ja va  2 s  . c o m
     * Converting String into BigDecimal
     * @param stringBigDecimal is any string
     * @return BigDecimal or null if that string is not number
     */
    public static BigDecimal saveConvertStringToBigDecimal(
            String stringBigDecimal) {

        final DecimalFormat format = new DecimalFormat();

        format.setParseBigDecimal(true);

        BigDecimal result = null;

        try {

            final Number number = format.parse(stringBigDecimal);

            if (format.isParseBigDecimal()) {
                result = new BigDecimal(number.toString());
            }

        } catch (ParseException ignore) {
        }

        return result;

    }
}

Related Tutorials