Convert Object to BigDecimal : BigDecimal « Data Type « Java

Convert Object to BigDecimal


import java.math.BigDecimal;
import java.math.BigInteger;

 * Utility methods for math classes
 * @author etirelli
public class MathUtils {
    public static BigDecimal getBigDecimal( Object value ) {
        BigDecimal ret = null;
        if( value != null ) {
            if( value instanceof BigDecimal ) {
                ret = (BigDecimal) value;
            } else if( value instanceof String ) {
                ret = new BigDecimal( (String) value );
            } else if( value instanceof BigInteger ) {
                ret = new BigDecimal( (BigInteger) value );
            } else if( value instanceof Number ) {
                ret = new BigDecimal( ((Number)value).doubleValue() );
            } else {
                throw new ClassCastException("Not possible to coerce ["+value+"] from class "+value.getClass()+" into a BigDecimal.");
        return ret;



Related examples in the same category

1.Round a double
2.Create Big Decimal Values via a long
3.Create a BigDecimal vis string
4.Multiply one BigDecimal to another BigDecimal
5.Subtract from one BigDecimal another BigDecimal
6.Divide one BigDecimal from another BigDecimal
7.Negate a BigDecimal
8.Setting the Decimal Place of a Big Decimal Value
9.Truncates the big decimal value
10.Do math operation for BigDecimal
11.Operate with big decimal values
12.Round a double by setting the scale
13.Create Big Decimal Values via a string
14.Calculation with BigDecimal
15.Parse BigDecimal
16.Formats BigDecimal into a SQL floating-point literal
17.Value is rounded using the given method which is any method defined in BigDecimal
18.Round the given value to the specified number of decimal places. The value is rounded using the BigDecimal.ROUND_HALF_UP method.
19.BigDecimal and BigInteger sqare root
20.BigDecimal quadratic