Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    /**
     * Returns a number rounded off to the given number of decimals.
     *
     * @param value    the value to round off.
     * @param decimals the number of decimals.
     * @return a number rounded off to the given number of decimals.
     */
    public static double getRounded(double value, int decimals) {
        final double factor = Math.pow(10, decimals);

        return Math.round(value * factor) / factor;
    }

    /**
     * Returns a rounded off number.
     * <p/>
     * <ul>
     * <li>If value is exclusively between 1 and -1 it will have 2 decimals.</li>
     * <li>If value if greater or equal to 1 the value will have 1 decimal.</li>
     * </ul>
     *
     * @param value the value to round off.
     * @return a rounded off number.
     */
    public static double getRounded(double value) {
        if (value < 1d && value > -1d) {
            return getRounded(value, 2);
        } else {
            return getRounded(value, 1);
        }
    }
}