Files
knowledge-kit/Chapter1 - iOS/1.47.md
2020-02-25 17:46:51 +08:00

129 lines
3.9 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 金融 App 金额格式化
> 在一些金融类的 App 中,对于表示金额类的字符串,通常需要进行格式化后再显示出来。例如:
>
> 0 显示为0.00
>
> 123 显示为123.00
>
> 123.456 显示为123.46
>
> 102000 显示为102,000.00
>
> 10204500 显示为10,204,500.00
>
> 它的规则为:个位数起每隔三位数字添加一个逗号,同时保留两位小数,也称为“千分位格式”。
### 方法一
首先根据小数点 `.` 将传入的字符串分割为两部分,整数部分和小数部分(如果没有小数点,则补 `.00`,如果有多个小数点则报金额格式错误)。对于小数部分,只取前两位;然后对整数部分字符串进行遍历,从右到左,每三位数前插入一个逗号 `,`,最后再把两部分拼接起来,代码大致如图 1 和图 2 所示。
```
- (void)method1{
NSArray *temps = @[@"0",@"123",@"123.456",@"102000",@"10204500"];
self.pricelabel.text = [self moneyFromat:temps[arc4random()%5]];
}
- (NSString *)moneyFromat:(NSString *)money{
if (!money || money.length == 0) {
return money;
}
BOOL hasPoint = NO;
if ([money rangeOfString:@"."].length > 0) {
hasPoint = YES;
}
NSMutableString *pointMoney = [NSMutableString stringWithString:money];
if (hasPoint == NO) {
[pointMoney appendFormat:@".00"];
}
NSArray *moneys = [pointMoney componentsSeparatedByString:@"."];
if (moneys.count > 2) {
return pointMoney;
}
else if (moneys.count == 1) {
return [NSString stringWithFormat:@"%@.00",moneys[0]];
}
else {
//整数部分每隔3位插入一个“,”
NSString *frontMoney = [self stringFromToThreeBit:moneys[0]];
if ([frontMoney isEqualToString:@""]) {
frontMoney = @"0";
}
//拼接整数部分和消暑部分
NSString *backMoney = moneys[1];
if (backMoney.length == 1) {
return [NSString stringWithFormat:@"%@.%@",frontMoney,backMoney];
}
else if (backMoney.length > 2) {
return [NSString stringWithFormat:@"%@.%@",frontMoney,[backMoney substringToIndex:2]];
}
else {
return [NSString stringWithFormat:@"%@.%@",frontMoney,backMoney];
}
}
}
- (NSString *)stringFromToThreeBit:(NSString *)string{
NSString *tempString = [string stringByReplacingOccurrencesOfString:@"," withString:@""];
NSMutableString *mutableString = [NSMutableString stringWithString:tempString];
NSInteger n = 2;
if (mutableString.length > 3) {
for (NSUInteger i = mutableString.length - 3; i > 0; i--) {
n++;
if (n == 3) {
[mutableString insertString:@"," atIndex:i];
n = 0;
}
}
}
return mutableString;
}
```
### 方法二
其实,苹果提供了 NSNumberFormatter 用来处理 NSString 和 NSNumber 之间的转化,可以满足基本的数字形式的格式化。我们通过设置 NSNumberFormatter 的 `numberStyle``positiveFormat` 属性,即可实现上述功能,非常简洁。
```objective-c
- (void)method2{
NSArray *temps = @[@"0",@"123",@"123.456",@"102000",@"10204500"];
self.pricelabel.text = [self formatDecimalNumber:temps[arc4random()%5]];
}
- (NSString *)formatDecimalNumber:(NSString *)string{
if (!string || string.length == 0) {
return string;
}
NSNumber *number = @([string doubleValue]);
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = kCFNumberFormatterDecimalStyle;
formatter.positiveFormat = @"###,##0.00";
formatter.positiveFormat = @"###,##0.00";
NSString *amountString = [formatter stringFromNumber:number];
return amountString;
}
```
### [参考资料](https://www.jianshu.com/p/817029422a72)