Sep 25

Create an instance of DecimalFormat with the required format and use its format() method to format the double value. See the DecimalFormat javadoc for details on specifying format.

double d = 1.23456789;

// Use 2 decimal places

NumberFormat numberFormat = 
   new DecimalFormat("#.##");

String s = numberFormat.format(d);
System.out.println(s);   // Outputs: 1.23 

written by objects \\ tags: , ,

Leave a Reply

You must be logged in to post a comment.