Convert Object to BigDecimal : BigDecimal « Data Type « Java Tutorial






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;
    }



}








2.46.BigDecimal
2.46.1.Round a double
2.46.2.Create Big Decimal Values via a long
2.46.3.Create a BigDecimal vis string
2.46.4.Multiply one BigDecimal to another BigDecimal
2.46.5.Subtract from one BigDecimal another BigDecimal
2.46.6.Divide one BigDecimal from another BigDecimal
2.46.7.Negate a BigDecimal
2.46.8.Setting the Decimal Place of a Big Decimal Value
2.46.9.Truncates the big decimal value
2.46.10.Do math operation for BigDecimal
2.46.11.Operate with big decimal values
2.46.12.Create Big Decimal Values via a string
2.46.13.Convert Object to BigDecimal