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;
/*******************************************************************************
 *  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.
 * *****************************************************************************
 *    __  _    _  ___//w ww  .j a v a 2s.  co  m
 *   (  )( \/\/ )/ __)
 *   /__\ \    / \__ \
 *  (_)(_) \/\/  (___/
 *
 *  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 {
    /**
     * Encodes real integer value into a string by offsetting and zero-padding
     * number up to the specified number of digits.  Use this encoding method if the data
     * range set includes both positive and negative values.
     *
     * @param number integer to be encoded
     * @param maxNumDigits maximum number of digits in the largest absolute value in the data set
     * @param offsetValue offset value, has to be greater than absolute value of any negative number in the data set.
     * @return string representation of the integer
     */
    public static String encodeRealNumberRange(int number, int maxNumDigits, int offsetValue) {
        long offsetNumber = number + offsetValue;
        String longString = Long.toString(offsetNumber);
        int numZeroes = maxNumDigits - longString.length();
        StringBuffer strBuffer = new StringBuffer(numZeroes + longString.length());
        for (int i = 0; i < numZeroes; i++) {
            strBuffer.insert(i, '0');
        }
        strBuffer.append(longString);
        return strBuffer.toString();
    }

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

    /**
     * Encodes real float value into a string by offsetting and zero-padding
     * number up to the specified number of digits.  Use this encoding method if the data
     * range set includes both positive and negative values.
     *
     * @param number float to be encoded
     * @param maxDigitsLeft maximum number of digits left of the decimal point in the largest absolute value in the data set
     * @param maxDigitsRight maximum number of digits right of the decimal point in the largest absolute value in the data set, i.e. precision
     * @param offsetValue offset value, has to be greater than absolute value of any negative number in the data set.
     * @return string representation of the integer
     */
    public static String encodeRealNumberRange(float number, int maxDigitsLeft, int maxDigitsRight,
            int offsetValue) {
        int shiftMultiplier = (int) Math.pow(10, maxDigitsRight);
        long shiftedNumber = (long) Math.round(number * shiftMultiplier);
        long shiftedOffset = offsetValue * shiftMultiplier;
        long offsetNumber = shiftedNumber + shiftedOffset;
        String longString = Long.toString(offsetNumber);
        int numBeforeDecimal = longString.length();
        int numZeroes = maxDigitsLeft + maxDigitsRight - numBeforeDecimal;
        StringBuffer strBuffer = new StringBuffer(numZeroes + longString.length());
        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(10, maxDigitsRight));
        //        System.out.println("shiftMultiplier=" + shiftMultiplier);
        BigDecimal shiftedNumber = number.multiply(shiftMultiplier);
        //        System.out.println("shiftedNumber=" + shiftedNumber);
        shiftedNumber = shiftedNumber.setScale(0, BigDecimal.ROUND_HALF_UP);
        //        System.out.println("shiftedNumber rounded=" + shiftedNumber);
        BigDecimal shiftedOffset = offsetValue.multiply(shiftMultiplier);
        //        System.out.println("shiftedOffset=" + shiftedOffset);
        BigDecimal offsetNumber = shiftedNumber.add(shiftedOffset);
        //        System.out.println("offsetNumber=" + offsetNumber);
        String longString = offsetNumber.toString();
        //        System.out.println("shifted string=" + longString);
        int numBeforeDecimal = longString.length();
        int numZeroes = maxDigitsLeft + maxDigitsRight - numBeforeDecimal;
        StringBuffer strBuffer = new StringBuffer(numZeroes + longString.length());
        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)