Java BigDecimal from toBigDecimal(Object val)

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

Description

to Big Decimal

License

Open Source License

Declaration

public static BigDecimal toBigDecimal(Object val) 

Method Source Code


//package com.java2s;
/*-------------------------------------------------------------------------------------------------
 _______ __   _ _______ _______ ______  ______
 |_____| | \  |    |    |______ |     \ |_____]
 |     | |  \_|    |    ______| |_____/ |_____]
    /*w  w  w . j  a  v a 2s . c  o m*/
 Copyright (c) 2016, antsdb.com and/or its affiliates. All rights reserved. *-xguo0<@
    
 This program is free software: you can redistribute it and/or modify it under the terms of the
 GNU Affero General Public License, version 3, as published by the Free Software Foundation.
    
 You should have received a copy of the GNU Affero General Public License along with this program.
 If not, see <https://www.gnu.org/licenses/agpl-3.0.txt>
-------------------------------------------------------------------------------------------------*/

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

public class Main {
    public static BigDecimal toBigDecimal(Object val) {
        if (val == null) {
            return null;
        }
        if (val instanceof Float) {
            return new BigDecimal((Float) val);
        }
        if (val instanceof Double) {
            return new BigDecimal((Double) val);
        }
        if (val instanceof BigInteger) {
            return new BigDecimal((BigInteger) val);
        }
        if (val instanceof Boolean) {
            return new BigDecimal(((Boolean) val) ? 1 : 0);
        }
        if (val instanceof Number) {
            return new BigDecimal(((Number) val).longValue());
        }
        throw new IllegalArgumentException();
    }
}

Related

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