解决了我的问题:只有当您知道您尝试格式化的 NSDecimal 将只是一个没有小数的整数时才能使用,因此请确保在对 NSDecimal 进行任何数学运算时四舍五入。
-(NSString *)returnFormattedString:(NSDecimal)nsDecimalToFormat{
NSMutableArray *formatArray = [NSMutableArray arrayWithObjects:@"%.2f",@"%.1f",@"%.0f",nil];
NSMutableArray *suffixes = [NSMutableArray arrayWithObjects:@"k",@"M",@"B",@"T",@"Qa",@"Qi",@"Sx",@"Sp",@"Oc",@"No",@"De",@"Ud",@"Dud",@"Tde",@"Qde",@"Qid",@"Sxd",@"Spd",@"Ocd",@"Nvd",@"Vi",@"Uvi",@"Dvi",@"Tvi", nil];
int dick = [suffixes count];
NSLog(@"count %i",dick);
NSString *string = NSDecimalString(&nsDecimalToFormat, _usLocale);
NSString *formatedString;
NSUInteger characterCount = [string length];
if (characterCount > 3) {
NSString *trimmedString=[string substringToIndex:3];
float a;
a = 100.00/(pow(10, (characterCount - 4)%3));
int remainder = (characterCount-4)%3;
int suffixIndex = (characterCount + 3 - 1)/3 - 2;
NSLog(@"%i",suffixIndex);
if(suffixIndex < [suffixes count]){
NSString *formatSpecifier = [formatArray[remainder] stringByAppendingString:suffixes[suffixIndex]];
formatedString= [NSString stringWithFormat:formatSpecifier, [trimmedString floatValue] / a];
}
else {
formatedString = @"too Big";
}
}
else{
formatedString = string;
}
return formatedString;
}