Int double Summary

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.