1 Packagecom;2 3 Public classT2 {4 5 Public Static voidMain (string[] args) {6 7System.out.println (Calculateprofit (0.233));8System.out.println (Calculateprofit (0.235));9System.out.println (Calculateprofit (0.237));TenSystem.out.println (Calculateprofit (0.2)); One A } - - /** the * Keep double type decimal after two bits, not rounded, directly take decimal after two bits such as: 10.1269 return: 10.12 - * - * @paramDoublevalue - * @return + */ - Public StaticString Calculateprofit (Doubledoublevalue) { + //Keep 2 decimal places AJava.text.DecimalFormat DF =NewJava.text.DecimalFormat ("0.00"); atString result =Df.format (doublevalue); - returnresult; - } - -}
Operation Result:
0.23
0.23
0.24
0.20
Other:
1: actually what I want to do is to round up, but after running the discovery unexpectedly is five to six into, if you have a good way to comment area tell me.
2:decimalformat for this type of hair:
DecimalFormat This type of formatted number is returned after the string, with the # number format, the number of bits is not enough when the 0, with 0 format the number of bits is not enough time to fill 0.
Related Blogs Double data holds two decimal digits two: Intercept decimal immediately after two bits without rounding
Double data reserved Two decimal places 1:5 six in