Java Formatter Usage getRoundedString(double value, int powerOf10)

Here you can find the source of getRoundedString(double value, int powerOf10)

Description

get Rounded String

License

Open Source License

Declaration

public static String getRoundedString(double value, int powerOf10) 

Method Source Code

//package com.java2s;
/*//from   w  w  w . j a va  2s  . co m
 *  RapidMiner
 *
 *  Copyright (C) 2001-2014 by RapidMiner and the contributors
 *
 *  Complete list of developers available at our web site:
 *
 *       http://rapidminer.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 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 Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/.
 */

import java.util.Formatter;

import java.util.Locale;

public class Main {
    public static String getRoundedString(double value, int powerOf10) {
        StringBuilder builder = new StringBuilder();
        if (powerOf10 < Integer.MAX_VALUE - 100) {
            value = roundToPowerOf10(value, powerOf10);
        }
        Formatter formatter = new Formatter(builder, Locale.getDefault());
        String format;
        if (powerOf10 < 0) {
            format = "%." + (-powerOf10) + "f";
        } else {
            format = "%.0f";
        }
        formatter.format(format, value);
        return builder.toString();
    }

    public static double roundToPowerOf10(double value, int powerOf10) {
        if (Double.isInfinite(value) || Double.isNaN(value)) {
            return value;
        }

        value *= Math.pow(10, -powerOf10);
        value = Math.round(value);
        value /= Math.pow(10, -powerOf10);
        return value;
    }
}

Related

  1. generateMacOnIncrease(final String baseMac, final long l)
  2. getCommitCounterStr(final long commitCounter)
  3. getFormatedTime(double timeInSeconds)
  4. getPropertiesString(Properties myProps)
  5. GetRawView(String string)
  6. incrementAlpha(String suppliedPrefix, String lastNumber, int numberLength)
  7. listToString(List list, String separator)
  8. long2Mac(final long macAddress)
  9. millisToHumanTime(long milliseconds)