Here you can find the source of formatDouble(double d, int n)
Parameter | Description |
---|---|
d | number to be formatted |
n | number of digits after decimal point |
public static String formatDouble(double d, int n)
//package com.java2s; //License from project: Open Source License public class Main { /**/* w w w .j a v a 2 s .c o m*/ * Formats a double to a given number of digits past the decimal point and * cuts the tailing zeroes * * @param d * number to be formatted * @param n * number of digits after decimal point * @return formatted string */ public static String formatDouble(double d, int n) { String s = String.format("%." + n + "f", d); int i = s.length() - 1; // cut tailing zeros while (s.charAt(i) == '0') { s = s.substring(0, i); i--; } // check if decimal point reached if (s.charAt(i) == '.') s = s.substring(0, i); return s; } }