Java BigInteger Calculate getValueWithUnit(BigInteger value, final int places)

Here you can find the source of getValueWithUnit(BigInteger value, final int places)

Description

Returns the given number as a string complete with unit (e.g., kilo, mega, etc.), to the specififed number of decimal places.

License

Open Source License

Declaration

public static String getValueWithUnit(BigInteger value, final int places) 

Method Source Code


//package com.java2s;
/*/*w w  w.  j a  va2 s  . c o  m*/
 * #%L
 * VisBio application for visualization of multidimensional biological
 * image data.
 * %%
 * Copyright (C) 2002 - 2014 Board of Regents of the University of
 * Wisconsin-Madison.
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 2 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-2.0.html>.
 * #L%
 */

import java.math.BigInteger;

public class Main {
    /** Units for use with getProductWithUnit method. */
    private static final String[] UNITS = { "", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta",
            "xona", "weka", "vunda", "uda", "treda", "sorta", "rinta", "quexa", "pepta", "ocha", "nena", "minga",
            "luma" };

    /**
     * Returns the given number as a string complete with unit (e.g., kilo, mega,
     * etc.), to the specififed number of decimal places.
     */
    public static String getValueWithUnit(BigInteger value, final int places) {
        if (value == null)
            return null;
        String s = "1";
        for (int i = 0; i < places; i++)
            s += "0";
        final BigInteger scale = new BigInteger(s);
        value = value.multiply(scale);

        int unit = 0;
        final BigInteger kilo = new BigInteger("1024");
        final BigInteger half = new BigInteger("512");
        final BigInteger stop = kilo.multiply(scale);
        while (value.compareTo(stop) >= 0) {
            final BigInteger[] bi = value.divideAndRemainder(kilo);
            value = bi[0];
            if (bi[1].compareTo(half) >= 0)
                value = value.add(BigInteger.ONE);
            unit++;
        }
        final BigInteger[] bi = value.divideAndRemainder(scale);
        String dec = bi[1].toString();
        while (dec.length() < places)
            dec = "0" + dec;
        final String u = unit < UNITS.length ? UNITS[unit] : ("(2^" + (3 * unit) + ")");
        String result = bi[0].toString();
        if (places > 0)
            result += "." + dec;
        result += " " + u;
        return result;
    }

    /** Adds two N-D vectors. */
    public static float[] add(final float[] v1, final float[] v2) {
        // v1 and v2 should have same lengths
        if (v1.length != v2.length)
            return null;
        final int len = v1.length;
        final float[] r = new float[v1.length];
        for (int i = 0; i < v1.length; i++) {
            r[i] = v1[i] + v2[i];
        }
        return r;
    }
}

Related

  1. getQosFlowId(short tableId, BigInteger dpId, int lportTag)
  2. getRadixNumberInString(BigInteger integerNumber, int radix)
  3. getRandomBigInteger(int bits)
  4. getRandomInteger(BigInteger n, Random rand)
  5. getSize(BigInteger number)
  6. getVersionString(BigInteger versionNumber)
  7. greater(BigInteger i1, BigInteger i2)
  8. hexEncode(BigInteger bigInteger)
  9. increment(BigInteger integer)