Swift3.0数字格式化(⾦额逗号分隔)
版权声明:本⽂为博主原创⽂章,未经博主允许不得转载。 /C_calary/article/details/52895762
umberFo rm a 1.N umber Fo r ma tter 设置数字显⽰格式
OC版本
//初始化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
ppt课件}
//输⼊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
//使⽤⽅法
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]]]; }