public static double round ( double val , int places ) { long factor = ( long ) Math . pow ( 10 , places ) ; // Shift the decimal the correct number of places // to the right. val = val * factor ; // Round to the nearest integer. long tmp = Math . round ( val ) ; // Shift the decimal the correct number of places // back to the left. return ( double ) tmp / factor ; }
Round a double value to a specified number of decimal places .