Java BigDecimal from toBigDecimal(Object value)

Here you can find the source of toBigDecimal(Object value)

Description

Convert an Object to a BigDecimal.

License

Apache License

Declaration

public static BigDecimal toBigDecimal(Object value) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.math.BigDecimal;

public class Main {
    /**/*ww w . j ava 2s . c  o  m*/
     * Convert an Object to a BigDecimal.
     */
    public static BigDecimal toBigDecimal(Object value) {
        if (value == null)
            return null;
        if (value instanceof BigDecimal)
            return (BigDecimal) value;
        if (value instanceof String) {
            if ("".equals((String) value))
                return null;
            return new BigDecimal((String) value);
        }
        if (value instanceof Number)
            return new BigDecimal(((Number) value).doubleValue());

        return new BigDecimal(value.toString());
    }

    /**
     * Convert an Object to a double, or 0 if it is null.
     */
    public static double doubleValue(Object value) {
        if (value == null)
            return 0.0;
        return toDouble(value).doubleValue();
    }

    /**
     * Convert an Object to a Double.
     */
    public static Double toDouble(Object value) {
        if (value == null)
            return null;
        if (value instanceof Double)
            return (Double) value;
        if (value instanceof String) {
            if ("".equals((String) value))
                return null;
            return new Double((String) value);
        }
        if (value instanceof Number)
            return new Double(((Number) value).doubleValue());

        return new Double(value.toString());
    }
}

Related

  1. toBigDecimal(Object obj)
  2. toBigDecimal(Object obj)
  3. toBigDecimal(Object object)
  4. toBigDecimal(Object val)
  5. toBigDecimal(Object value)
  6. toBigDecimal(String _str)
  7. toBigDecimal(String s)
  8. toBigDecimal(String s)
  9. toBigDecimal(String s, int scale)