IOS when the requested data is a double type, it loses precision and removes 0 of the decimal point

Source: Internet
Author: User

The data that is requested first becomes a string, and the string is first converted to a double type

double fdouble = [str doublevalue];

And then set the number of digits after the decimal point

[NSString stringWithFormat:@ "%.1f", fdouble];

Emphasis: Provides a nssing extension that passes in the decimal place that needs to be preserved, returning a string. and remove the end of the 0.

#import<Foundation/Foundation.h>@interfacensstring (Eliminatezero)//integer must pass-(NSString *) Eliminatezerowithdouble: (Nsinteger) integer;@end#import "nsstring+eliminatezero.h"@implementationnsstring (Eliminatezero)-(NSString *) Eliminatezerowithdouble: (nsinteger) integer{nsstring*str =[self copy]; DoubleFdouble =[str doublevalue]; NSString*Ftotal; Switch(integer) { Case 1: Ftotal= [NSString stringWithFormat:@"%.1f", fdouble];  Break;  Case 2: Ftotal= [NSString stringWithFormat:@"%.2f", fdouble];  Break;  Case 3: Ftotal= [NSString stringWithFormat:@"%.3f", fdouble];  Break;  Case 4: Ftotal= [NSString stringWithFormat:@"%.4f", fdouble];  Break;  Case 5: Ftotal= [NSString stringWithFormat:@"%.5f", fdouble];  Break; default:             Break; }     while([Ftotal Hassuffix:@"0"]) {ftotal= [Ftotal substringtoindex:[ftotal length]-1]; }        if([Ftotal Hassuffix:@"."]) {ftotal= [Ftotal substringtoindex:[ftotal length]-1]; }        returnFtotal; }@end

IOS when the requested data is a double type, it loses precision and removes 0 of the decimal point

Related Article

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.