Swift3.0数字格式化(⾦额逗号分隔)1.NumberFormatter设置数字显⽰格式
//初始化NumberFormatter
let format = NumberFormatter()
//设置numberStyle(有多种格式)
format.numberStyle = .decimal
//转换后的string
let string = format.string(from: NSNumber(value: 1234567.89))
//打印
print(string)
//显⽰样式和输出样式
带蛇字的成语numberStyle:
public enum Style : UInt {
ca none //1234567.89
ca decimal//1,234,567.89
ca currency //¥1,234,567.89
ca percent //123,456,789%
ca scientific //1.23456789E6
ca spellOut //⼀百⼆⼗三万四千五百六⼗七点⼋九
@available(iOS 9.0, *)
ca ordinal //第123,4568
@available(iOS 9.0, *)
ca currencyISOCode //CNY1,234,567.89
我的朋友圈作文
@available(iOS 9.0, *)
ca currencyPlural//1,234,567.89⼈民币
@available(iOS 9.0, *)
ca currencyAccounting//¥1,234,567.89
斧声烛影}
//输⼊123456789分别输出
// 123456789
// 123,456,789
//¥123,456,789.00
//12345678900%
//1.23456789E8
/
/⼀亿⼆千三百四⼗五万六千七百⼋⼗九高考奋斗格言
加强阵地建设//第1,2345,6789
//CNY123,456,789.00
//123,456,789.00⼈民币
//¥123,456,789.00
OC版本
//使⽤⽅法
NSNumberFormatter *format = [[NSNumberFormatter alloc] init];
format.numberStyle = NSNumberFormatterDecimalStyle;
NSString *formatStr = [format stringFromNumber:[NSNumber numberWithDouble:1009090.09]];
//封装的保留两位的逗号分隔⽅法
+ (NSString *)changeMoneyPositiveFormat:(NSString *)text{
if(!text || [text floatValue] == 0){
return @"0.00";
生蚝怎样做
}
if (text.floatValue < 1000) {
return [NSString stringWithFormat:@"%.2f",text.floatValue];黑便的原因有哪些
};
风筝涂色NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter tPositiveFormat:@",###.00;"];
return [numberFormatter stringFromNumber:[NSNumber numberWithDouble:[text doubleValue]]]; }