Java - Write code to round a double value by precision

Requirements

Write code to round a double value by precision

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        double value = 42.45678;
        int precision = 2;
        System.out.println(round(value, precision));
    }//from   ww w .j av a 2  s . c o  m

    public static double round(double value, int precision) {
        if (precision > 0) {
            double exp = Math.pow(10, precision);
            return Math.round(value * exp) / exp;
        } else {
            return Math.round(value);
        }
    }
}