iOS 中数字的格式化处理
2015-10-22 10:54
567 查看
最近越来越多的App 中对数字采用分割数字格式,看起来更直观。iOS中NSNumberFormatter 可以帮我们从容处理,使用方式类似于NSDateFormatter。代码如下:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; formatter.numberStyle = NSNumberFormatterDecimalStyle; NSString *string = [formatter stringFromNumber:[NSNumber numberWithInt:123456789]]; NSLog(@"Formatted number string:%@",string); 输出结果为: Formatted number string:123,456,789
其中NSNumberFormatter类有个属性numberStyle,它是一个枚举型,设置不同的值可以输出不同的数字格式。该枚举包括:
enum {
NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle,
NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle,
NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle,
NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle,
NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle,
NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle
};
typedef NSUInteger NSNumberFormatterStyle;
各个枚举对应输出数字格式的效果如下:
Formatted number string:123456789
Formatted number string:123,456,789
Formatted number string:¥123,456,789.00
Formatted number string:-539,222,988%
Formatted number string:1.23456789E8
Formatted number string:一亿二千三百四十五万六千七百八十九
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 什么是Mac OS X?跟Linux有什么区别
- mac下安装mysql(转载)
- mysql 修改编码 Linux/Mac/Unix/通用(杜绝修改后无法启动的情况!)
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- Linux vs Mac:Linux 比 Mac 好的 7 个原因
- 苹果mac OS X上安装metasploit
- Flex中对表格某列的值进行数字格式化并求百分比添加%
- Validation of viewstate MAC failed.的解决方法
- Mac平台中编译安装Lua运行环境及Hello Lua实例
- 在Mac OS中安装Lua的教程
- asp格式化日期时间格式的代码
- vbs Windows系统改变或修改网卡的MAC地址的脚本与软件第1/2页
- MAC官方菜单纯CSS实现灰色会换色
- 在Mac OS下使用Node.js的简单教程