Java BigDecimal from toBigDecimal(Integer num)

Here you can find the source of toBigDecimal(Integer num)

Description

to Big Decimal

License

Open Source License

Declaration

public static BigDecimal toBigDecimal(Integer num) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
    public static BigDecimal toBigDecimal(Integer num) {
        try {/*from  w  w w.j a v a 2s . co m*/
            return roundBigDecimal(new BigDecimal(num));
        } catch (Exception ignored) {
        }
        return null;
    }

    public static BigDecimal toBigDecimal(Double num) {
        try {
            return roundBigDecimal(new BigDecimal(num));
        } catch (Exception ignored) {
        }
        return null;
    }

    public static BigDecimal toBigDecimal(Float num) {
        try {
            return roundBigDecimal(new BigDecimal(num));
        } catch (Exception ignored) {
        }
        return null;
    }

    public static BigDecimal toBigDecimal(String num) {
        try {
            return roundBigDecimal(new BigDecimal(num));
        } catch (Exception ignored) {
        }
        return null;
    }

    public static BigDecimal toBigDecimal(String num, Integer scale) {
        try {
            return roundBigDecimal(new BigDecimal(num), scale);
        } catch (Exception ignored) {
        }
        return null;
    }

    public static BigDecimal toBigDecimal(Object num) {
        try {
            return roundBigDecimal(num != null ? new BigDecimal(num.toString()) : null);
        } catch (Exception ignored) {
        }
        return null;
    }

    public static BigDecimal roundBigDecimal(BigDecimal num) {
        return roundBigDecimal(num, 2);
    }

    public static BigDecimal roundBigDecimal(BigDecimal num, int scale) {
        if (num != null) {
            return num.setScale(scale, RoundingMode.HALF_UP);
        }
        return num;
    }
}

Related

  1. toBigDecimal(final Number number)
  2. toBigDecimal(final Number number)
  3. toBigDecimal(final String bigdecimal)
  4. toBigDecimal(final String string)
  5. toBigDecimal(final String value)
  6. toBigDecimal(JsonValue val)
  7. toBigDecimal(long seconds, int nanoseconds)
  8. toBigDecimal(Number n)
  9. toBigDecimal(Number n)