Java tutorial
//package com.java2s; //License from project: Apache License import java.math.BigDecimal; public class Main { public static float truncate(float f, int decimalPlaces) { float decimalShift = (float) Math.pow(10, decimalPlaces); return Math.round(f * decimalShift) / decimalShift; } public static BigDecimal round(double number, int decimal) { return new BigDecimal(number).setScale(decimal, BigDecimal.ROUND_HALF_UP); } }