Here you can find the source of formatDouble(double number, int decimalDigits)
Parameter | Description |
---|---|
number | a number value to be converted. |
decimalDigits | a number of digits after decimal point. |
public static String formatDouble(double number, int decimalDigits)
//package com.java2s; /******************************************************************************* * Copyright (c) 2007 Australian Nuclear Science and Technology Organisation. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/*from w ww . j ava 2s . c o m*/ * Danil Klimontov (Bragg Institute) - initial API and implementation *******************************************************************************/ import java.text.DecimalFormat; public class Main { /** * Converts the double value to string presentation. * @param number a number value to be converted. * @param decimalDigits a number of digits after decimal point. * @return string presentation of the number. */ public static String formatDouble(double number, int decimalDigits) { String pattern = "#0."; for (int i = 0; i < decimalDigits; i++) { pattern += "#"; } final DecimalFormat formatter = new DecimalFormat(pattern); return formatter.format(number); } }