Resolution of string to float type distortion in iOS

Source: Internet
Author: User

Recently, in the process of doing the project, I stumbled across a problem, that is, the conversion of string and floating-point types. In the past, it was through [NSString stringwithformat:@ "%d", Goodcount] this way, and didn't care too much. Customer feedback in a project today after entering a very long number, the data display of the floating-point type is inconsistent with the input. After repeated testing, it was found that float type can only support to 8-bit, that is to say 12345678 such a number through the float type is no problem, but if more than eight bit after nsstring *[email protected] "  1234567890 ";   NSLog (@ "Data type%.2f,%.2f", [Temstr floatvalue],[temstr doublevalue]); Print output data type 1234567936.00, 1234567890.00. The original float type has a maximum capacity of 8 bits, whereas a double type has a capacity of 16 bits, so the conversion of strings and floating-point types is best used in the project development process with a double type.  However, a double type is distorted if it exceeds 16 bits (nsstring *[email protected] "123456789012345678"; NSLog (@ "Data type%.2f,%.2f", [Temstr floatvalue],[temstr doublevalue]); Print output data type 123456790519087104.00,123456789012345680.00), but it's enough.

Resolution of string to float type distortion in iOS

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.