method One : The String class comes with the method String.Format ("%.2f", 1.2548); The numbers in "%.2f" decide to keep several
method Two : Format the method
private static final Java.text.DecimalFormat df = new Java.text.DecimalFormat ("0.00%");p rivate static final Java.text.DecimalFormat DF2 = new Java.text.DecimalFormat ("0.00");p ublic static String format (Double data) { Synchronized (DF) {return Df.format (data);}} public static String FORMAT2 (Double data) {synchronized (DF2) {return Df2.format (data);}}
method Three : Float a =1.23466643f;
int b = (int) (a*10000);
A = (float) b/10000;
System.out.println (a);
method Four : You can not round up the method of preserving the decimal point is one of the special cases where the acquisition takes into account the number of 0 in place
Double num = 1;
Integer b = (int) (num*100000);
String a =b/100000+ "."
+ (b%100000>=10000?b%100000/10000: ("0"))
+ (b%10000>=1000?b%10000/1000: ("0"))
+ (b%1000>=100?b%1000/100: ("0"))
+ (B%100>=10?B%100/10: ("0" +b%10));
System.out.println (a);
Several ways to preserve decimal points in Java