1,金额格式化
(1)需要注意当金额或者数字为负数是需要单独处理
代码如下:
/*string 金额格式化(整数部分为NSNumberFormatter,小数部分为手动)*/
+ (NSString *)stringFamat:(NSString *)number
{
if([number isEqualToString:@"--"]){
return number;
}
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";
}
2,金额大写需要分为整数部分和小数部分来计算分别是两个函数
需要大写的汉字等可以对其进行宏定义
#define SPDiv 10000
#define SPUnit @[@"分", @"角"]
#define SPLowScale @[@"",@"拾", @"佰", @"仟"]
#define SPLargeScale @[@"",@"万",@"亿",@"兆"]
#define SPBase @[@"零", @"壹", @"贰", @"叁", @"肆", @"伍", @"陆", @"柒", @"捌", @"玖"]
+ (NSString *)stringFamatWithCapital:(NSString *)money
{
if (money && [money doubleValue] > 0.0f) {
NSMutableString *capital = [[NSMutableString alloc]init];
NSString *formatString = [NSString stringWithFormat:@"%.2f",[money doubleValue]];
NSString *par_int = [formatString substringToIndex:formatString.length - 3];
NSString *par_dig = [formatString substringFromIndex:formatString.length - 2];
BOOL isZero = ([par_int integerValue] == 0);
capital = [self moneyInteger:par_int];
[capital appendString:[self moneyDecimal:par_dig isZeroInt:isZero]];
// if ([capital isEqualToString:@"元整"]) {
// capital = [NSMutableString stringWithString:@"零元整"];
// }
return capital;
}
else
return @"零元整";
}
小数部分代码:
//金额小数部分转换
+ (NSString *)moneyDecimal:(NSString *)par_dig isZeroInt:(BOOL)isZero
{
NSMutableString *dec = [[NSMutableString alloc]init];
NSInteger dig = [par_dig integerValue];
if (dig == 0) {
[dec appendString:@"元整"];
}
else {
if (!isZero) {
[dec appendString:@"元"];
}
//0.01 is 壹分
NSString *temp = [NSString stringWithFormat:@"%li",(long)dig];
if (dig % 10 != 0) {
for (NSInteger i = 0; i < temp.length; i++) {
[dec appendString:SPBase[[[temp substringWithRange:NSMakeRange(i, 1)]integerValue]]];
[dec appendString:SPUnit[temp.length - 1 -i]];
}
}
else {
[dec appendString:SPBase[[[temp substringWithRange:NSMakeRange(0, 1)]integerValue]]];
[dec appendString:SPUnit[1]];
}
}
return dec;
}
整数部分代码:
//金额整数部分转换
+ (NSMutableString *)moneyInteger:(NSString *)par_int
{
NSMutableString *capital = [[NSMutableString alloc]init];
NSInteger count = -1;
NSMutableArray *items = [NSMutableArray array];
NSInteger lenth = par_int.length;
long long int val_int = [par_int longLongValue];
while (lenth > 0) {
NSInteger tem = val_int % SPDiv;
[items addObject:[NSString stringWithFormat:@"%li",(long)tem]];
val_int = val_int / SPDiv;
lenth -= 4;
count ++;
}
// NSLog(@"%@",items);
for (NSInteger i = count; i >= 0; i --) {
NSString *str = items[i];
NSInteger zeroCount = 0;
BOOL shouldZero = count > 0 && str.length < 4 && i < count;
for (NSInteger j = 0; j < str.length; j ++) {
NSInteger k = [[str substringWithRange:NSMakeRange(j, 1)]integerValue];
if (k != 0) {
if (zeroCount != 0 || shouldZero) {
[capital appendString:SPBase[0]];
zeroCount = 0;
shouldZero = NO;
}
[capital appendString:SPBase[k]];
[capital appendString:SPLowScale[str.length - 1 - j]];
}
else {
zeroCount ++;
}
}
if ([str integerValue] != 0) {
[capital appendString:SPLargeScale[i]];
}
}
return capital;
}