Java examples for Language Basics:Number Format
Formats a number using the general number format with exactly three decimal places:
import java.text.NumberFormat; public class Main { public static void main(String[] args) { double x = 12345.6789; NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMinimumFractionDigits(3);/*from ww w .j a va2s .c om*/ nf.setMaximumFractionDigits(3); System.out.println(nf.format(x)); } }