素材巴巴 > 程序开发 >

[iOS]数字每隔3位用逗号分隔

程序开发 2023-09-12 14:11:32

demo:http://download.csdn.net/download/u012881779/9960776

之前做电商类APP时,针对稍大的金额展示时,需要每隔千位添加逗号便于用户识别。

逻辑比较简单,这里按需要写了一个可选前后缀的一个转换方法:

方式一

/**价格转换为每隔3位用逗号分割show 是否显示小数点后面*/
 + (NSString *)changePriceWithNumber:(float)value showPoint:(BOOL)show {NSString *valueStr = @"";NSString *format = @"";if (show) {valueStr = [NSString stringWithFormat:@"%.2f", value];format = @",###.##";} else {valueStr = [NSString stringWithFormat:@"%.f", value];format = @",###";}NSDecimalNumber *decNumber = [NSDecimalNumber decimalNumberWithString:valueStr];NSNumberFormatter *numberFormatter =   [[NSNumberFormatter alloc] init];[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];[numberFormatter setPositiveFormat:format];return [numberFormatter stringFromNumber:decNumber];
 }

方式二


 #import "DMViewController.h"@interface DMViewController ()
 @property (weak, nonatomic) IBOutlet UITextField *titleText;
 @property (weak, nonatomic) IBOutlet UILabel *numLab;@end@implementation DMViewController- (IBAction)tapButtonAction:(id)sender {UIButton *tempBut = (UIButton *)sender;if (tempBut.isSelected) {tempBut.selected = NO;_numLab.text = @"label";} else {tempBut.selected = YES;NSString *newNumber = [self separateNumberUseCommaWith:_titleText.text];_numLab.text = newNumber;}
 }// 将数字转为每隔3位整数由逗号“,”分隔的字符串
 - (NSString *)separateNumberUseCommaWith:(NSString *)number {// 前缀NSString *prefix = @"¥";// 后缀NSString *suffix = @"元";// 分隔符NSString *divide = @",";// 是否为负数BOOL isMinus = NO;if ([number floatValue] < 0) {isMinus = YES;}NSString *integer = @"";NSString *radixPoint = @"";BOOL contains = NO;if ([number containsString:@"."]) {contains = YES;// 若传入浮点数,则需要将小数点后的数字分离出来NSArray *comArray = [number componentsSeparatedByString:@"."];integer = [comArray firstObject];radixPoint = [comArray lastObject];} else {integer = number;}// 将整数按各个字符为一组拆分成数组NSMutableArray *integerArray = [[NSMutableArray alloc] init];for (int i = 0; i < integer.length; i ++) {NSString *subString = [integer substringWithRange:NSMakeRange(i, 1)];if ([subString isEqualToString:@"-"]) {continue;}[integerArray addObject:subString];}// 将整数数组倒序每隔3个字符添加一个逗号“,”NSString *newNumber = @"";for (NSInteger i = 0 ; i < integerArray.count ; i ++) {NSString *getString = @"";NSInteger index = (integerArray.count-1) - i;if (integerArray.count > index) {getString = [integerArray objectAtIndex:index];}BOOL result = YES;if (index == 0 && integerArray.count%3 == 0) {result = NO;}if ((i+1)%3 == 0 && result) {newNumber = [NSString stringWithFormat:@"%@%@%@",divide,getString,newNumber];} else {newNumber = [NSString stringWithFormat:@"%@%@",getString,newNumber];}}if (contains) {newNumber = [NSString stringWithFormat:@"%@.%@",newNumber,radixPoint];}if (![prefix isEqualToString:@""]) {newNumber = [NSString stringWithFormat:@"%@%@",prefix,newNumber];}if (![suffix isEqualToString:@""]) {newNumber = [NSString stringWithFormat:@"%@%@",newNumber,suffix];}if (isMinus) {newNumber = [NSString stringWithFormat:@"-%@",newNumber];}return newNumber;
 }@end


示意图:

 

 

 

 


标签:

素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。