Java BigDecimal Create bigDecimalFromBytes(byte[] decimalBytes, int scale)

Here you can find the source of bigDecimalFromBytes(byte[] decimalBytes, int scale)

Description

Returns a BigDecimal from the given bytes and scale.

License

Apache License

Parameter

Parameter Description
decimalBytes the bytes of the decimal value's integral value, assumed to be two's complement, big-endian
scale the scale of the decimal value

Return

a constructed from the decimalBytes and scale

Declaration

public static BigDecimal bigDecimalFromBytes(byte[] decimalBytes, int scale) 

Method Source Code

//package com.java2s;
/*/* ww  w .jav  a  2s.com*/
 * Copyright 2017 StreamSets Inc.
 *
 * 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.math.BigInteger;

public class Main {
    /**
     * Returns a {@link BigDecimal} from the given bytes and scale.  The bytes must adhere to the format that Avro
     * stores decimals in.
     * <br>
     * Avro stores decimal values as two's complement, big-endian for the integral portion, then the decimal place
     * separately (via the scale).
     *
     * @see <a href="https://avro.apache.org/docs/1.8.1/spec.html#Decimal">Avro documentation</a>
     *
     * @param decimalBytes the bytes of the decimal value's integral value, assumed to be two's complement, big-endian
     * @param scale the scale of the decimal value
     * @return a {@link BigDecimal} constructed from the decimalBytes and scale
     */
    public static BigDecimal bigDecimalFromBytes(byte[] decimalBytes, int scale) {
        final BigInteger bigInt = new BigInteger(decimalBytes);
        return new BigDecimal(bigInt, scale);
    }
}

Related

  1. bigDecimal(Number num)
  2. bigDecimal(Object object)
  3. bigDecimalFromString(String s)
  4. bigDecimalValue(final Number number)
  5. bigDecimalValueOf(Number n)
  6. createBigDecimal(double v)