Java BigDecimal to convertToBigDecimal(final Object o)

Here you can find the source of convertToBigDecimal(final Object o)

Description

convert To Big Decimal

License

Open Source License

Declaration

public static BigDecimal convertToBigDecimal(final Object o) 

Method Source Code


//package com.java2s;
/*/* w  w w . j  a  v  a2  s  .  c  o  m*/
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
 * Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 * or from the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * Copyright (c) 2001 - 2013 Object Refinery Ltd, Pentaho Corporation and Contributors..  All rights reserved.
 */

import java.math.BigDecimal;

public class Main {
    public static BigDecimal convertToBigDecimal(final Object o) {
        if (o instanceof Number) {
            return convertToBigDecimal((Number) o);
        }
        return null;
    }

    public static BigDecimal convertToBigDecimal(final Number number) {
        if (number == null) {
            throw new NullPointerException();
        }

        if (number instanceof BigDecimal) {
            return (BigDecimal) number;
        } else if (number instanceof Integer || number instanceof Long) {
            return new BigDecimal(number.longValue());
        }
        return new BigDecimal(number.toString());
    }
}

Related

  1. convertNullToBigDecimal(Object orgStr)
  2. convertRadiansToDegrees(BigDecimal radians)
  3. convertRating(BigDecimal rating)
  4. convertSecondsToMillis(BigDecimal seconds)
  5. convertStringToBigDecimal(String strValue)
  6. convertToBigDecimal(Object sourceValue)
  7. convertToBigDecimal(String s)
  8. convertToBigDecimal(String val)
  9. convertToBigDecimal(String value)