Package decimal;
Import Java.math.BigDecimal;
Import Java.text.DecimalFormat;
Import Java.text.NumberFormat; /** * Java several ways to retain several decimal places and add thousands separator * <p>ClassName:TestDecimal</p> * <p>description: Keep a few decimal places </p> * <p>Author:Administrator</p> * <p>date:2017 Year April 26 </p> * * Public class Testdecimal {public
static void Main (string[] args) {//mode a small number of less than 4 people are several, more than 4 people leave only 4-digit double DD1 = 911.911;
Double dd2 = 911.911911;
DecimalFormat df = new DecimalFormat ("#.0000");
DD1 = double.parsedouble (Df.format (DD1));
DD2 = double.parsedouble (Df.format (DD2));
System.out.println (DD1);
System.out.println (DD2);
System.out.println ("////////");
The way two decimal places less than 4 people with 0 complement, more than 4 people leave only 4 digits double ds1 = 911.911;
Double ds2 = 911.911911;
String RESULT1 = String.Format ("%.4f", DS1);
String result2 = String.Format ("%.4f", DS2);
System.out.println (RESULT1); System.out.println (RESULT2);
System.out.println ("////////");
The mode of the three decimal places of less than 4 people are several, more than 4 people left only 4, and rounded double db1 = 911.911;
Double DB2 = 911.91186;
BigDecimal bd1 = new BigDecimal (DB1);
BigDecimal bd2 = new BigDecimal (DB2);
DB1 = Bd1.setscale (4, bigdecimal.round_half_up). Doublevalue ();
DB2 = Bd2.setscale (4, bigdecimal.round_half_up). Doublevalue ();
System.out.println (DB1);
SYSTEM.OUT.PRINTLN (DB2);
System.out.println ("////////");
Mode four number of decimal places less than 4 people are several, more than 4 people left only 4 double dn1 = 911.911;
Double dn2 = 911.911911;
NumberFormat NF = numberformat.getnumberinstance ();
Nf.setmaximumfractiondigits (4);
String str1 = Nf.format (DN1);
String str2 = Nf.format (DN2);
System.out.println (STR1);
System.out.println (STR2);
System.out.println ("////////");
Add thousand separator double n = 1000.3; DecimalFormat DF2 = new DecimalforMat ("#,###.00");
String m = Df2.format (n);
System.out.print (m); }
}