1. In Android, how does one solve the number of digits of the double type data * operation result? For example, 3.2*3 should be 9.6, but the output result is 9.6000000000001. This is because no range is added to the result.
Double A = 3.2;
Double B = 3;
Numberformat. getinstance (). Format (A * B); default value:
2. Reserved digits of the Double Type
Method 1 Decimalformat df = new decimalformat (". 00 ");
//. 00 indicates that the last two digits are retained.
System. Out. println (F. Format (x/y). tostring (); // call f. Format (x/y) and convert it to a string.
Method 2
Double num1 = 10;
Double num2 = 3;
So num1/num2 = 3. 3333333333 ....
How can we retain it to the nth place?
Use # And 0 to control String Conversion
Format: (num1/num2). tostring ("#. 00") indicates that two decimal places are retained.
(Num1/num2 ). tostring ("#. 000 ") indicates that three decimal places are retained, but note that if there is no number in the third digit after the decimal point, the third digit is also displayed, but the value of this position is 0, such as 3.140
(Num1/num2 ). tostring ("#. 00 # ") also indicates that three decimal places are retained, but note that if there is no number in the third digit after the decimal point, the third digit is not displayed, but is only displayed to the second digit, such as 3.14
Note: The double type number is 3.14.
Tostring ("0 #. 00") = "display value: 03.14
Tostring ("#. 00") = "display value: 3.14
Summary: # = If # indicates that no number exists in this position, it is null.
0 = If 0 indicates that there is no number at this position, 0 is used to represent the number at this position.
Method 3