Java BigDecimal valueOf(final BigDecimal value)

Here you can find the source of valueOf(final BigDecimal value)

Description

value Of

License

Apache License

Declaration

public static BigDecimal valueOf(final BigDecimal value) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

import java.util.Arrays;

public class Main {
    /**/*ww w  .j a  v  a2s .c  om*/
     * adopted from <a href=
     * "https://forum.processing.org/two/discussion/10384/bigdecimal-to-byte-array">
     * orthoptera (apr 2015)</a>
     * 
     * @param raw big-endian two's-complement binary representation with first 4
     *            bytes representing the scale. The value of the
     *            {@code BigDecimal} is
     *            <tt>(unscaledVal &times; 10<sup>-scale</sup>)</tt>.
     * @return a {@link BigDecimal}
     * @see BigInteger#BigInteger(byte[])
     * @see BigDecimal#BigDecimal(BigInteger,int)
     */
    public static BigDecimal valueOf(final byte[] raw) {
        // read scale from first 4 bytes
        final int scale = (raw[0] & 0xFF) << 24 | (raw[1] & 0xFF) << 16 | (raw[2] & 0xFF) << 8 | (raw[3] & 0xFF);
        final byte[] subset = Arrays.copyOfRange(raw, 4, raw.length);
        final BigInteger unscaled = new BigInteger(subset);
        return new BigDecimal(unscaled, scale);
    }

    public static BigDecimal valueOf(final BigDecimal value) {
        return value;
    }

    public static BigDecimal valueOf(final BigInteger value) {
        return valueOf(value.longValueExact());
    }

    public static BigDecimal valueOf(final long value) {
        return BigDecimal.valueOf(value);
    }

    public static BigDecimal valueOf(final Number value) {
        return value instanceof BigDecimal ? valueOf((BigDecimal) value)
                : value instanceof Long || value instanceof Integer || value instanceof Short
                        || value instanceof Byte ? valueOf(value.longValue())
                                : value instanceof BigInteger ? valueOf((BigInteger) value)
                                        : BigDecimal.valueOf(value.doubleValue());
    }
}

Related

  1. tryParseBigDecimal(Object val, BigDecimal defaultVal)
  2. tryToStoreAsIntegerBigDecimal(Object ob)
  3. tryToStoreAsRealBigDecimal(Object ob)
  4. unformattedFromBigDecimal(BigDecimal n)
  5. unformattedToBigDecimal(String str)
  6. writeBigDecimal(BigDecimal val, DataOutput out)
  7. writeBigDecimal(final OutputStream out, @Nullable final BigDecimal value)
  8. zeroOrMore(BigDecimal decimal)