Java BigDecimal from toBigDecimal(Object obj)

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

Description

Convert number object to BigDecimal

License

Open Source License

Parameter

Parameter Description
obj a parameter

Declaration

public static BigDecimal toBigDecimal(Object obj) 

Method Source Code

//package com.java2s;
/**/*  w  ww . j  a v  a2s .  co m*/
 * Copyright (c) 2010-2014, openHAB.org and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.math.BigDecimal;

public class Main {
    /**
     * Convert number object to BigDecimal
     * @param obj
     * @return
     */
    public static BigDecimal toBigDecimal(Object obj) {

        if (obj instanceof Integer) {
            return BigDecimal.valueOf((int) obj);
        } else if (obj instanceof Long) {
            return BigDecimal.valueOf((long) obj);
        } else if (obj instanceof Short) {
            return BigDecimal.valueOf((short) obj);
        } else if (obj instanceof Double) {
            return BigDecimal.valueOf((double) obj);
        } else if (obj instanceof Float) {
            return BigDecimal.valueOf((float) obj);
        } else if (obj instanceof BigDecimal) {
            return (BigDecimal) obj;
        }

        return null;
    }
}

Related

  1. toBigDecimal(Number number)
  2. toBigDecimal(Number number)
  3. toBigDecimal(Number number, int scale)
  4. toBigDecimal(Number price)
  5. toBigDecimal(Object n)
  6. toBigDecimal(Object obj)
  7. toBigDecimal(Object object)
  8. toBigDecimal(Object val)
  9. toBigDecimal(Object value)