Here you can find the source of format(double number, int precision)
public static double format(double number, int precision)
//package com.java2s; /*//from w w w . j av a 2 s .c o m * Copyright 2011-2016 ZXC.com All right reserved. This software is the confidential and proprietary information of * ZXC.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into with ZXC.com. */ public class Main { public static double format(double number, int precision) { int tmp = 1; for (int i = 0; i < precision; i++) { tmp *= 10; } int value = (int) Math.round(number * tmp); return (value * 1d) / tmp; } }