- (NSString *)formatToTwoDecimal:(id)count {
NSString *originNumber;
if ([count isKindOfClass:[NSString class]] || [count isKindOfClass:[NSNumber class]]) {
NSInteger i = [count integerValue];
originNumber = [NSString stringWithFormat:@"%ld",(long)I];
} else {
return @"¥0.00";
}
NSDecimalNumber *one = [NSDecimalNumber decimalNumberWithString:originNumber];
NSDecimalNumber *two = [NSDecimalNumber decimalNumberWithString:@"100"];
NSDecimalNumber *thr = [one decimalNumberByDividingBy:two];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
formatter.positiveFormat = @",###.##";
NSString *money = [formatter stringFromNumber:[NSNumber numberWithDouble:[thr doubleValue]]];
NSString *result = [NSString stringWithFormat:@"¥%@",money];
if (![result containsString:@"."]) { //被整除的情况
result = [NSString stringWithFormat:@"%@.00",result];
} else { //小数不足两位
NSArray *array = [result componentsSeparatedByString:@"."];
NSString *subNumber = array.lastObject;
if (subNumber.length == 1) {
result = [NSString stringWithFormat:@"%@.%@0",array.firstObject, array.lastObject];
}
}
return result;
}
1235875-b9f5976682eb328a.png
网友评论