Java BigDecimal to bigDecimalToByte(BigDecimal num)

Here you can find the source of bigDecimalToByte(BigDecimal num)

Description

This method will convert a big decimal value to bytes

License

Apache License

Parameter

Parameter Description
num a parameter

Declaration

public static byte[] bigDecimalToByte(BigDecimal num) 

Method Source Code


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

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

public class Main {
    /**//from w  w w. j  a va  2s .c  o m
     * This method will convert a big decimal value to bytes
     *
     * @param num
     * @return
     */
    public static byte[] bigDecimalToByte(BigDecimal num) {
        BigInteger sig = new BigInteger(num.unscaledValue().toString());
        int scale = num.scale();
        byte[] bscale = new byte[] { (byte) (scale) };
        byte[] buff = sig.toByteArray();
        byte[] completeArr = new byte[buff.length + bscale.length];
        System.arraycopy(bscale, 0, completeArr, 0, bscale.length);
        System.arraycopy(buff, 0, completeArr, bscale.length, buff.length);
        return completeArr;
    }
}

Related

  1. bigDecimalToBytes(BigDecimal decimal)
  2. bigDecimalToDbString(BigDecimal bd)
  3. bigDecimalToInteger(BigDecimal value)
  4. bigDecimaltoJSONString(Object obj)