Java tutorial
//package com.java2s; public class Main { public static double toPrecision(double v, int precision) { double factor = Math.pow(10, precision); return Math.round(v * factor) / factor; } }