Java BigDecimal Encode encodeRealNumberRange(BigDecimal number, int maxNumDigits, BigDecimal offsetValue)

Here you can find the source of encodeRealNumberRange(BigDecimal number, int maxNumDigits, BigDecimal offsetValue)

Description

encode Real Number Range

License

Apache License

Declaration

public static String encodeRealNumberRange(BigDecimal number, int maxNumDigits, BigDecimal offsetValue) 

Method Source Code

//package com.java2s;
/**/*w  w w.  ja v a  2  s  .c  om*/
 * *****************************************************************************
 * Copyright 2007 Amazon Technologies, 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://aws.amazon.com/apache2.0 This file 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.
 * ***************************************************************************** __ _ _ ___ ( )( \/\/ )/ __) /__\ \ / \__ \ (_)(_) \/\/ (___/
 *
 * Amazon Simple DB Java Library API Version: 2007-11-07 Generated: Fri Jan 18 01:13:17 PST 2008
 *
 */

import java.math.BigDecimal;

public class Main {
    private static final int BASE = 10;

    public static String encodeRealNumberRange(BigDecimal number, int maxNumDigits, BigDecimal offsetValue) {
        final BigDecimal offsetNumber = number.add(offsetValue);
        final String longString = offsetNumber.toString();
        final int numZeroes = maxNumDigits - longString.length();
        final int paddedSize = numZeroes + longString.length();
        final StringBuilder strBuffer = new StringBuilder(paddedSize);
        for (int i = 0; i < numZeroes; i++) {
            strBuffer.insert(i, '0');
        }
        strBuffer.append(longString);

        return strBuffer.toString();
    }

    public static String encodeRealNumberRange(BigDecimal number, int maxDigitsLeft, int maxDigitsRight,
            BigDecimal offsetValue) {
        BigDecimal shiftMultiplier = new BigDecimal(Math.pow(BASE, maxDigitsRight));
        BigDecimal shiftedNumber = number.multiply(shiftMultiplier);
        shiftedNumber = shiftedNumber.setScale(0, BigDecimal.ROUND_HALF_UP);
        final BigDecimal shiftedOffset = offsetValue.multiply(shiftMultiplier);
        final BigDecimal offsetNumber = shiftedNumber.add(shiftedOffset);
        String longString = offsetNumber.toString();
        final int numBeforeDecimal = longString.length();
        final int numZeroes = maxDigitsLeft + maxDigitsRight - numBeforeDecimal;
        final int paddedSize = numZeroes + longString.length();
        final StringBuilder strBuffer = new StringBuilder(paddedSize);
        for (int i = 0; i < numZeroes; i++) {
            strBuffer.insert(i, '0');
        }
        strBuffer.append(longString);
        return strBuffer.toString();
    }
}

Related

  1. encodeImage(BufferedImage bufferedImage, File file)
  2. encodePackedBCD(BigDecimal v, int decimals, int length)
  3. encodeRealNumberRange(BigDecimal number, int maxNumDigits, BigDecimal offsetValue)