Returns the given number as a string complete with unit (e.g., kilo, mega, etc.), to the specified number of decimal places. - Java java.lang

Java examples for java.lang:Math Number

Description

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

Demo Code

/*/*w w w.jav  a  2s. 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%
 */
//package com.java2s;
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 specified 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 Tutorials