Java BigDecimal from toBigDecimal(Object value)

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

Description

to Big Decimal

License

Open Source License

Declaration

public static BigDecimal toBigDecimal(Object value) 

Method Source Code

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

import java.math.BigDecimal;

public class Main {
    public static BigDecimal toBigDecimal(Object value) {
        return toBigDecimal(value, new BigDecimal(0));
    }/* ww  w.  j a v a  2 s  .c  o m*/

    public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue) {
        if (value == null) {
            return defaultValue;
        }
        if (value instanceof BigDecimal) {
            BigDecimal decimal = (BigDecimal) value;
            return decimal;
        }
        return new BigDecimal(toDouble(value));
    }

    public static double toDouble(Object obj) {
        return toDouble(obj, 0d);
    }

    public static double toDouble(Object obj, double defaultValue) {
        if (obj == null) {
            return defaultValue;
        }

        if (obj instanceof Number) {
            Number number = (Number) obj;
            return number.doubleValue();
        }
        String value = toString(obj);
        try {
            return Double.parseDouble(value);
        } catch (Exception e) {
        }
        return defaultValue;
    }

    public static String toString(Object value) {
        if (value == null) {
            return "";
        }
        return value.toString().trim();
    }
}

Related

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