Java BigDecimal to convertBigDecimalToSQLBigNum(BigDecimal bd, int targetLength, int targetScale)

Here you can find the source of convertBigDecimalToSQLBigNum(BigDecimal bd, int targetLength, int targetScale)

Description

convert Big Decimal To SQL Big Num

License

Apache License

Declaration

public static byte[] convertBigDecimalToSQLBigNum(BigDecimal bd, int targetLength, int targetScale) 

Method Source Code


//package com.java2s;
//  Licensed under the Apache License, Version 2.0 (the "License");

import java.math.BigDecimal;

public class Main {
    static private final int[] powersOfTen = { 10, 100, 1000, 10000 };

    public static byte[] convertBigDecimalToSQLBigNum(BigDecimal bd, int targetLength, int targetScale) {
        byte[] sourceData = bd.setScale(targetScale, BigDecimal.ROUND_DOWN).unscaledValue().toString().getBytes(); // add
        // trailing
        // 0s,//from  www .j  a  v a 2  s .c om
        // remove decimal point,
        // get the chars
        byte[] targetData = new byte[targetLength];
        int[] targetInShorts = new int[targetLength / 2];

        int length;
        int temp;
        int tarPos = 1;

        // remove leading 0s and sign character
        int zeros = 0;
        while (zeros < sourceData.length && (sourceData[zeros] == '0' || sourceData[zeros] == '-'))
            zeros++;

        // convert from characters to values
        for (int i = zeros; i < sourceData.length; i++)
            sourceData[i] -= '0';

        length = sourceData.length - zeros; // we have a new length

        // iterate through 4 bytes at a time
        for (int i = 0; i < length; i += 4) {
            int temp1 = 0;
            int j = 0;

            // get 4 bytes worth of data or as much that is left
            for (j = 0; j < 4 && i + j < length; j++)
                temp1 = temp1 * 10 + sourceData[zeros + i + j];

            int power = powersOfTen[j - 1]; // get the power of ten based on how
            // many digits we got

            temp = targetInShorts[0] * power + temp1; // move the current
            // digits over and then
            // add our new value in
            targetInShorts[0] = temp & 0xFFFF; // we save only up to 16bits --
            // the rest gets carried over

            // we do the same thing for the rest of the digits now that we have
            // an upper bound
            for (j = 1; j < targetInShorts.length; j++) {
                int t = (temp & 0xFFFF0000) >> 16;
                temp = targetInShorts[j] * power + t;

                targetInShorts[j] = temp & 0xFFFF;
            }

            int carry = (temp & 0xFFFF0000) >> 16;
            if (carry > 0) {
                targetInShorts[tarPos++] = carry;
            }
        }

        // convert the data back to bytes
        for (int i = 0; i < targetInShorts.length; i++) {
            //         targetData[i * 2] = (byte) ((targetInShorts[i] & 0xFF00) >> 8);
            //         targetData[i * 2 + 1] = (byte) (targetInShorts[i] & 0xFF);
            targetData[i * 2] = (byte) (targetInShorts[i] & 0xFF);
            targetData[i * 2 + 1] = (byte) ((targetInShorts[i] & 0xFF00) >> 8);
        }

        // add sign
        if ((bd.signum() < 0))
            targetData[targetData.length - 2] |= 0x80;

        return targetData;
    }
}

Related

  1. bigDecimalToLong(BigDecimal value)
  2. bigDecimalToStellarBalance(final BigDecimal balance)
  3. bigDecimalToString(BigDecimal bigDecimal)
  4. bigDecimalToString(BigDecimal dec)
  5. convertBigDecimalTodouble(BigDecimal bigDecimalVal)
  6. convertBigDecimalToString(BigDecimal bigValue)
  7. convertDoubleToBigDecimal(Double value)
  8. convertDoubleToBigDecimal(final Double score, final int decimalPlaces)
  9. convertFeetToInches(BigDecimal feet)