Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    /**
     * Rounds a number to a given number of significant decimal digits.
     * Note that the number will be left with *only* this number of
     * significant digits regardless of magnitude, e.g. 12345 to 3 digits
     * will be 12300, whereas 0.12345 will be 0.123.
     *
     * @param value the value to round off.
     * @param n     the number of significant decimal digits desired.
     * @return a rounded off number.
     */
    public static double roundToSignificantDigits(double value, int n) {
        if (value == 0.0) {
            return 0.0;
        }

        final double d = Math.ceil(Math.log10(value < 0.0 ? -value : value));
        final int power = n - (int) d;

        final double magnitude = Math.pow(10.0, power);
        final long shifted = Math.round(value * magnitude);
        return shifted / magnitude;
    }
}