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