Java BigDecimal Create toBigDecimal(final Object o)

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

Description

to Big Decimal

License

Apache License

Declaration

public static BigDecimal toBigDecimal(final Object o) 

Method Source Code


//package com.java2s;
/*//from  ww w  . j  ava2  s . c o m
 * Copyright 2004-2012 the Seasar Foundation and the Others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

import java.math.BigDecimal;
import java.text.SimpleDateFormat;

public class Main {

    public static BigDecimal toBigDecimal(final Object o) {
        return toBigDecimal(o, null);
    }

    public static BigDecimal toBigDecimal(final Object o, final String pattern) {
        if (o == null) {
            return null;
        } else if (o instanceof BigDecimal) {
            return (BigDecimal) o;
        } else if (o instanceof java.util.Date) {
            if (pattern != null) {
                return new BigDecimal(new SimpleDateFormat(pattern).format(o));
            }
            return new BigDecimal(Long.toString(((java.util.Date) o).getTime()));
        } else if (o instanceof String) {
            final String s = (String) o;
            if (s.isEmpty()) {
                return null;
            }
            return normalize(new BigDecimal(s));
        } else {
            return normalize(new BigDecimal(o.toString()));
        }
    }

    public static String toString(final BigDecimal dec) {
        return dec.toPlainString();
    }

    private static BigDecimal normalize(final BigDecimal dec) {
        return new BigDecimal(dec.toPlainString());
    }
}

Related

  1. getDecimalValues(String[] values, String dataType)
  2. nullToBigDecimalZero(final Object obj)
  3. nullToZero(BigDecimal decimal)
  4. nullToZero(BigDecimal num)
  5. toBig(Object o)
  6. toBigDecimal(final Object value)
  7. toBigDecimal(Object o)
  8. toBigDecimal(Object obj)
  9. toBigDecimal(String bigDecimal)