Java BigDecimal getUnscaledBytes(BigDecimal bd)

Here you can find the source of getUnscaledBytes(BigDecimal bd)

Description

get Unscaled Bytes

License

Open Source License

Declaration

static public byte[] getUnscaledBytes(BigDecimal bd) throws IOException 

Method Source Code

//package com.java2s;
/* This file is part of VoltDB.
 * Copyright (C) 2008-2013 VoltDB Inc./*from www  . j a  v  a2 s .co  m*/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with VoltDB.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;

import java.util.Arrays;

public class Main {
    /**
     * The scale of decimals in Volt
     */
    public static final int kDefaultScale = 12;
    /**
     * Array containing the smallest 16-byte twos complement value that is used
     * as SQL null.
     */
    private static final byte[] NULL_INDICATOR = new BigInteger(
            "-170141183460469231731687303715884105728").toByteArray();
    /**
     * Array of scale factors used to scale up <code>BigInteger</code>s retrieved from
     * <code>BigDecimal</code>s
     */
    private static final BigInteger scaleFactors[] = new BigInteger[] {
            BigInteger.ONE, BigInteger.TEN, BigInteger.TEN.pow(2),
            BigInteger.TEN.pow(3), BigInteger.TEN.pow(4),
            BigInteger.TEN.pow(5), BigInteger.TEN.pow(6),
            BigInteger.TEN.pow(7), BigInteger.TEN.pow(8),
            BigInteger.TEN.pow(9), BigInteger.TEN.pow(10),
            BigInteger.TEN.pow(11), BigInteger.TEN.pow(12) };

    static public byte[] getUnscaledBytes(BigDecimal bd) throws IOException {
        if (bd == null) {
            return Arrays.copyOf(NULL_INDICATOR, NULL_INDICATOR.length);
        }
        final int scale = bd.scale();
        final int precision = bd.precision();
        if (scale > 12) {
            throw new IOException("Scale of " + bd + " is " + scale
                    + " and the max is 12");
        }
        final int precisionMinusScale = precision - scale;
        if (precisionMinusScale > 26) {
            throw new IOException("Precision of " + bd
                    + " to the left of the decimal point is "
                    + precisionMinusScale + " and the max is 26");
        }
        final int scaleFactor = kDefaultScale - bd.scale();
        BigInteger unscaledBI = bd.unscaledValue().multiply(
                scaleFactors[scaleFactor]);
        boolean isNegative = false;
        if (unscaledBI.signum() < 0) {
            isNegative = true;
        }
        final byte unscaledValue[] = unscaledBI.toByteArray();
        if (unscaledValue.length > 16) {
            throw new IOException("Precision of " + bd + " is >38 digits");
        }
        return expandToLength16(unscaledValue, isNegative);
    }

    /**
     * Converts BigInteger's byte representation containing a scaled magnitude to a fixed size 16 byte array
     * and set the sign in the most significant byte's most significant bit.
     * @param scaledValue Scaled twos complement representation of the decimal
     * @param isNegative Determines whether the sign bit is set
     * @return
     */
    private static final byte[] expandToLength16(byte scaledValue[],
            final boolean isNegative) {
        if (scaledValue.length == 16) {
            return scaledValue;
        }
        byte replacement[] = new byte[16];
        if (isNegative) {
            java.util.Arrays.fill(replacement, (byte) -1);
        }
        for (int ii = 15; 15 - ii < scaledValue.length; ii--) {
            replacement[ii] = scaledValue[ii
                    - (replacement.length - scaledValue.length)];
        }
        return replacement;
    }
}

Related

  1. getScaledDouble(BigDecimal input)
  2. getSid(BigDecimal total, AtomicLong sid)
  3. getSignedBalance(BigDecimal balance)
  4. getTensVal(BigDecimal val)
  5. getTotalSum(List subTotals)
  6. getUpdateAccountsQuery(BigDecimal[] result, int id)
  7. getUserLink(BigDecimal id, String name)
  8. getValue(BigDecimal value, Class clazz)
  9. getValue(String key, Map valuesMap)