金额格式化
Posted 紫洁
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了金额格式化相关的知识,希望对你有一定的参考价值。
@interface NSString (RMB) - (NSString *)RMBFormatString; @end #import "NSString+RMB.h" @implementation NSString (RMB) - (NSString *)RMBFormatString { NSString *number = self; if ([number doubleValue] > 0) { number = [NSString stringWithFormat:@"%.2f",[number doubleValue]]; NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc]init]; numFormatter.numberStyle = NSNumberFormatterDecimalStyle; NSNumber *num = [NSNumber numberWithDouble:[number doubleValue]]; NSString *str = [NSString stringWithFormat:@"%@",[numFormatter stringFromNumber:num]]; if ([str rangeOfString:@"."].length > 0) { NSString *floatStr = [str componentsSeparatedByString:@"."][1]; if (floatStr.length == 1) { str = [NSString stringWithFormat:@"%@0",str]; } else{ str = [str substringToIndex:([str rangeOfString:@"."].location + 3)]; } return [NSString stringWithFormat:@"¥%@",str]; } else{ return [NSString stringWithFormat:@"¥%@.00",str]; } } else { return @"¥0.00"; } } @end
以上是关于金额格式化的主要内容,如果未能解决你的问题,请参考以下文章