@interface NSString (Helper)
/**
* 截取小數(shù)點(diǎn)位數(shù),最多保留digit位,且小數(shù)點(diǎn)后多余的0將被去掉.
*
* @param digit 小數(shù)點(diǎn)后最多保留位數(shù)
*
* @return 返回一個截取后的小數(shù)字符串對象
*/
- (NSString *)floatStringTruncateWithMaxReservedDecimalPointDigit:(NSInteger)digit;
@end
@implementation NSString (Helper)
- (NSString *)floatStringTruncateWithMaxReservedDecimalPointDigit:(NSInteger)digit
{
if (self.length == 0) {
return @"0";
}
if ([self rangeOfString:@"."].length == 0) { //不是小數(shù)
return self;
}
NSMutableString *formatString = [NSMutableString stringWithString:@"%.f"];
[formatString insertString:[NSString stringWithFormat:@"%ld", (long)digit] atIndex:2];
NSString *truncatedStr = [NSString stringWithFormat:formatString, self.floatValue];
NSString *rs = [NSString stringWithFormat:@"%@", @(truncatedStr.floatValue)];
return rs;
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者