public static void Main (string[] args) { //1. First multiply and then round, then divide; Double d = 62.31060027198647; Double D2 = Math.Round (d*100)/100.0; System.out.println ("Divide by math": "+ D2); 2. Rounding with the number of decimal places is accomplished by BigDecimal's Setscale (), which is converted to a BigDecimal object. Below this feasible BigDecimal bd = new BigDecimal (d); BigDecimal Bd2 = Bd.setscale (2, bigdecimal.round_half_up); System.out.println ("obtained by Bigdecimal.setscale:" + bd2); 3. DecimalFormat df = new DecimalFormat ("#.##") of string returned by Decimalformat.format; System.out.println ("obtained by Decimalformat.format:" + df.format (d)); 4. Through String.Format System.out.println ("Through StringFormat:" + String.Format ("%.2f", d));
After the completion of math to do division: 62.31//Through the Bigdecimal.setscale obtained: 62.31//Through the Decimalformat.format obtained: 62.31//through the stringformat:62.31
The second type of personal advice
Ava Double: Rounding and setting the number of decimal places