去除小数点后面无效的0

/// 输入字符串

- (NSString *)deleteEndZeroStr {

if([self rangeOfString:@"."].location !=NSNotFound)//

{

NSString *resultStr = @"";

for (NSUInteger i = self.length-1; i > 0; i--) {

unichar c = [self characterAtIndex:i];

if (c != '0' ) {

if (c =='.') {

resultStr = [self substringToIndex:i];

break;

}else {

resultStr = [self substringToIndex:i+1];

break;

}

}

}

return resultStr;

}

else

{

return self;

}

}

/// 输入浮点型

+ (NSString *)deletedEndZeroStrWithCGFloat:(CGFloat)num {

NSString *str = [NSString stringWithFormat:@"%.2f",num];

if([str rangeOfString:@"."].location !=NSNotFound)

{

NSString *resultStr = @"";

for (NSUInteger i = str.length-1; i > 0; i--) {

unichar c = [str characterAtIndex:i];

if (c != '0') {

if (c =='.') {

resultStr = [str substringToIndex:i];

break;

}else {

resultStr = [str substringToIndex:i+1];

break;

}

}

}

return resultStr;

}else {

return str;

}

}

推荐阅读更多精彩内容