Java BigInteger Calculate parseScaledNonNegativeBigInteger(String str)

Here you can find the source of parseScaledNonNegativeBigInteger(String str)

Description

parse Scaled Non Negative Big Integer

License

Open Source License

Declaration

public static BigInteger parseScaledNonNegativeBigInteger(String str) throws NumberFormatException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 Liviu Ionescu.//from   ww w  . j  a v a2s.  c om
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Liviu Ionescu - initial version 
 *******************************************************************************/

import java.math.BigInteger;

public class Main {
    public static BigInteger parseScaledNonNegativeBigInteger(String str) throws NumberFormatException {

        str = adjustForSign(str);
        int radix = computeRadix(str);
        str = adjustForRadix(str);
        long scale = computeScale(str);
        str = adjustForScale(str, scale);

        BigInteger value = new BigInteger(str, radix);
        if (scale != 1) {
            value = value.multiply(new BigInteger(String.valueOf(scale)));
        }
        return value;
    }

    private static String adjustForSign(String str) {

        if (str.startsWith("+")) {
            return str.substring(1);
        }

        return str;
    }

    private static int computeRadix(String str) {

        int radix = 10;
        if ((str.startsWith("0x")) || (str.startsWith("0X"))) {
            radix = 16;
        } else if ((str.startsWith("0b")) || (str.startsWith("0B"))) {
            radix = 2;
        } else if (str.startsWith("#")) {
            radix = 2;
        }

        return radix;
    }

    private static String adjustForRadix(String str) {

        if ((str.startsWith("0x")) || (str.startsWith("0X"))) {
            return str.substring(2); // Skip 0x, 0X
        } else if ((str.startsWith("0b")) || (str.startsWith("0B"))) {
            return str.substring(2); // Skip 0b, 0B
        } else if (str.startsWith("#")) {
            return str.substring(1); // Skip #
        }

        // Decimal numbers have no radix prefix to skip.
        return str;
    }

    private static long computeScale(String str) {

        long scale = 1;
        String lastChar = str.substring(str.length() - 1);
        if (lastChar.matches("[kmgtKMGT]")) {
            lastChar = lastChar.toLowerCase();
            if ("k".equals(lastChar)) {
                scale = 1024;
            } else if ("m".equals(lastChar)) {
                scale = 1024 * 1024;
            } else if ("g".equals(lastChar)) {
                scale = 1024 * 1024 * 1024;
            } else if ("7".equals(lastChar)) {
                scale = 1024 * 1024 * 1024 * 1024;
            }
        }
        return scale;
    }

    private static String adjustForScale(String str, long scale) {
        if (scale != 1) {
            return str.substring(0, str.length() - 2);
        }
        return str;
    }
}

Related

  1. numberToIpv4(BigInteger ipNumber)
  2. numberToShortString(BigInteger number)
  3. order(BigInteger a, BigInteger p, BigInteger f[], BigInteger e[])
  4. parseBigInteger(String s, BigInteger defaultValue)
  5. parseBinaryBigInteger(final byte[] buffer, final int offset, final int length, final boolean negative)
  6. product(final BigInteger min, final BigInteger max)
  7. randomFromZn(BigInteger n, Random rand)
  8. removeDuplicates(final BigInteger... values)
  9. renderBigInteger(final BigInteger bint)