Java BigDecimal from toBigDecimal(final Double d)

Here you can find the source of toBigDecimal(final Double d)

Description

Null save convert of a Double to a BigDecimal .

License

Open Source License

Parameter

Parameter Description
d the Double to convert.

Return

the .

Declaration

public static BigDecimal toBigDecimal(final Double d) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013, 2014, 2015 QPark Consulting  S.a r.l.
 * //from  w w  w .  j  a v  a 2  s  .  c o m
 * This program and the accompanying materials are made available under the 
 * terms of the Eclipse Public License v1.0. 
 * The Eclipse Public License is available at 
 * http://www.eclipse.org/legal/epl-v10.html.
 ******************************************************************************/

import java.math.BigDecimal;

import java.math.RoundingMode;

public class Main {
    /**
     * Null save convert of a {@link Double} to a {@link BigDecimal}.
     *
     * @param d
     *            the {@link Double} to convert.
     * @return the {@link BigDecimal}.
     */
    public static BigDecimal toBigDecimal(final Double d) {
        if (d != null) {
            return BigDecimal.valueOf(d);
        } else {
            return null;
        }
    }

    /**
     * Null save convert of a {@link Double} to a {@link BigDecimal} with a
     * scale. The scale is only set if the existing is larger than the given
     * one.
     *
     * @param d
     *            the double to convert.
     * @param scale
     *            the scale to reduce the {@link BigDecimal} to.
     * @return the {@link BigDecimal}.
     */
    public static BigDecimal toBigDecimal(final Double d, final int scale) {
        if (d != null) {
            BigDecimal bd = toBigDecimal(d);
            if (bd.scale() > scale) {
                bd = bd.setScale(scale, RoundingMode.HALF_UP);
            }
            return bd;
        } else {
            return null;
        }
    }

    /**
     * Null save convert of a {@link Float} to a {@link BigDecimal}.
     *
     * @param f
     *            the {@link Float} to convert.
     * @return the {@link BigDecimal}.
     */
    public static BigDecimal toBigDecimal(final Float f) {
        if (f != null) {
            return BigDecimal.valueOf(f);
        } else {
            return null;
        }
    }

    /**
     * Null save convert of a {@link Float} to a {@link BigDecimal} with a
     * scale. The scale is only set if the existing is larger than the given
     * one.
     *
     * @param f
     *            the double to convert.
     * @param scale
     *            the scale to reduce the {@link BigDecimal} to.
     * @return the {@link BigDecimal}.
     */
    public static BigDecimal toBigDecimal(final Float f, final int scale) {
        if (f != null) {
            BigDecimal bd = toBigDecimal(f);
            if (bd.scale() > scale) {
                bd = bd.setScale(scale, RoundingMode.HALF_UP);
            }
            return bd;
        } else {
            return null;
        }
    }

    /**
     * Null save convert of a {@link Integer} to a {@link BigDecimal}.
     *
     * @param i
     *            the {@link Integer} to convert.
     * @return the {@link BigDecimal}.
     */
    public static BigDecimal toBigDecimal(final Integer i) {
        if (i != null) {
            return BigDecimal.valueOf(i);
        } else {
            return null;
        }
    }

    /**
     * Null save convert of a {@link Short} to a {@link BigDecimal}.
     *
     * @param s
     *            the {@link Short} to convert.
     * @return the {@link BigDecimal}.
     */
    public static BigDecimal toBigDecimal(final Short s) {
        if (s != null) {
            return BigDecimal.valueOf(s);
        } else {
            return null;
        }
    }
}

Related

  1. toBigDecimal(Double d)
  2. toBigDecimal(Double d)
  3. toBigDecimal(double val)
  4. toBigDecimal(double[][] a)
  5. toBigDecimal(final byte value)
  6. toBigDecimal(final Number n)
  7. toBigDecimal(final Number number)
  8. toBigDecimal(final Number number)
  9. toBigDecimal(final Number number)