介绍

NSNumberFormatter 应该可以满足你对数据形式的一般需求,值得了解一下.

    NSNumber *num1 = [NSNumber numberWithDouble:1234567.8369];

    // ==================== 类方法 ====================

    // 四舍五入的整数
NSString *numberNoStyleStr = [NSNumberFormatter localizedStringFromNumber:num1 numberStyle:NSNumberFormatterNoStyle]; // 小数形式
NSString *numberDecimalStyleStr = [NSNumberFormatter localizedStringFromNumber:num1 numberStyle:NSNumberFormatterDecimalStyle]; // 货币形式 -- 本地化
NSString *numberCurrencyStyleStr = [NSNumberFormatter localizedStringFromNumber:num1 numberStyle:NSNumberFormatterCurrencyStyle]; // 百分数形式
NSString *numberPercentStyleStr = [NSNumberFormatter localizedStringFromNumber:num1 numberStyle:NSNumberFormatterPercentStyle]; // 科学计数
NSString *numberScientificStyleStr = [NSNumberFormatter localizedStringFromNumber:num1 numberStyle:NSNumberFormatterScientificStyle]; // 朗读形式 -- 本地化
NSString *numberSpellOutStyleStr = [NSNumberFormatter localizedStringFromNumber:num1 numberStyle:NSNumberFormatterSpellOutStyle]; // 序数形式 -- 本地化
NSString *numberOrdinalStyleStr = [NSNumberFormatter localizedStringFromNumber:num1 numberStyle:NSNumberFormatterOrdinalStyle]; // 货币形式 ISO -- 本地化
NSString *numberCurrencyISOStyleStr = [NSNumberFormatter localizedStringFromNumber:num1 numberStyle:NSNumberFormatterCurrencyISOCodeStyle]; // 货币形式 -- 本地化
NSString *numberCurrencyPluralStyleStr = [NSNumberFormatter localizedStringFromNumber:num1 numberStyle:NSNumberFormatterCurrencyPluralStyle]; // 会计计数 -- 本地化
NSString *numberCurrencyAccountingStyleStr = [NSNumberFormatter localizedStringFromNumber:num1 numberStyle:NSNumberFormatterCurrencyAccountingStyle]; NSLog(@"No Style = %@",numberNoStyleStr); // No Style = 1234568
NSLog(@"Decimal Style = %@",numberDecimalStyleStr); // Decimal Style = 1,234,567.837
NSLog(@"Currency Style = %@",numberCurrencyStyleStr); // Currency Style = $1,234,567.84
NSLog(@"Percent Style = %@",numberPercentStyleStr); // Percent Style = 123,456,784%
NSLog(@"Scientific Style = %@",numberScientificStyleStr); // Scientific Style = 1.2345678369E6
// Spell Out Style = one million two hundred thirty-four thousand five hundred sixty-seven point eight three six nine
NSLog(@"Spell Out Style = %@",numberSpellOutStyleStr);
NSLog(@"Ordinal Style = %@",numberOrdinalStyleStr); // Ordinal Style = 1,234,568th
NSLog(@"Currency ISO Style = %@",numberCurrencyISOStyleStr); // Currency ISO Style = USD1,234,567.84
NSLog(@"Currency plural Style = %@",numberCurrencyPluralStyleStr); // Currency plural Style = 1,234,567.84 US dollars
NSLog(@"Currency accounting Style = %@",numberCurrencyAccountingStyleStr); // Currency accounting Style = $1,234,567.84 // ==================== 定制 ==================== NSNumberFormatter *numberFormatter = [NSNumberFormatter new]; numberFormatter.numberStyle = NSNumberFormatterDecimalStyle; // 格式宽度
numberFormatter.formatWidth = 15; // 填充符
numberFormatter.paddingCharacter = @"?"; // 填充位置
numberFormatter.paddingPosition = kCFNumberFormatterPadBeforeSuffix;
numberFormatter.positiveSuffix = @"元"; NSLog(@"%@",[numberFormatter numberFromString:@"10000000元"]); // 10000000 // 缩放因子,你可以将一个数缩放指定比例,然后给其添加后缀,如传入一个3000,你希望表示为3千,就要用到这个属性
// 防止影响后面的测试,故注掉
// numberFormatter.multiplier = @1000; // NSLog(@"%@千",[numberFormatter numberFromString:@"1000"]); // 1千 // numberFormatter.multiplier = @0.001;
// numberFormatter.positiveSuffix = @"千";
// NSLog(@"%@",[numberFormatter stringFromNumber:@10000]); // 10千 // 机制不明确,负号,正号
// numberFormatter.negativeFormat = @"^";
// numberFormatter.positiveFormat = @"~0"; // 貌似没什么用
numberFormatter.allowsFloats = NO;
numberFormatter.alwaysShowsDecimalSeparator = NO;
numberFormatter.maximum = @1000;
numberFormatter.minimum = @100; // 小数点样式
numberFormatter.decimalSeparator = @"."; // 零的样式
numberFormatter.zeroSymbol = @"-"; // 前缀和后缀
numberFormatter.positivePrefix = @"!";
numberFormatter.positiveSuffix = @"元";
numberFormatter.negativePrefix = @"@";
numberFormatter.negativeSuffix = @"亏"; // 指定符号,与我们在前面类方法中说明的一致
NSLog(@"货币代码%@",numberFormatter.currencyCode); // 货币代码USD
NSLog(@"货币符号%@",numberFormatter.currencySymbol); // 货币符号$
NSLog(@"国际货币符号%@",numberFormatter.internationalCurrencySymbol); // 国际货币符号USD
NSLog(@"百分比符号%@",numberFormatter.percentSymbol); // 百分比符号%
NSLog(@"千分号符号%@",numberFormatter.perMillSymbol); // 千分号符号‰
NSLog(@"减号符号%@",numberFormatter.minusSign); // 减号符号-
NSLog(@"加号符号%@",numberFormatter.plusSign); // 加号符号+
NSLog(@"指数符号%@",numberFormatter.exponentSymbol); // 指数符号E // 整数最多位数
numberFormatter.maximumIntegerDigits = 10; // 整数最少位数
numberFormatter.minimumIntegerDigits = 2; // 小数位最多位数
numberFormatter.maximumFractionDigits = 3; // 小数位最少位数
numberFormatter.minimumFractionDigits = 1; // 数字分割的尺寸
numberFormatter.groupingSize = 4; // 除了groupingSize决定的尺寸外,其他数字位分割的尺寸
numberFormatter.secondaryGroupingSize = 2; // 最大有效数字个数
numberFormatter.maximumSignificantDigits = 12; // 最少有效数字个数
numberFormatter.minimumSignificantDigits = 3; NSLog(@"正数%@,负数%@",[numberFormatter stringFromNumber:@(+12135230.2346)],[numberFormatter stringFromNumber:@(-12135231.2346)]); // 正数!12,13,5230.2346元,负数@12,13,5231.2346亏
NSLog(@"零 = %@",[numberFormatter stringFromNumber:@(0)]); // 零 = - // 舍入值,比如以10为进位值,那么156就进位为160,154进位为150
numberFormatter.roundingIncrement = @10; // 舍入方式
numberFormatter.roundingMode = kCFNumberFormatterRoundHalfUp;
NSLog(@"%@",[numberFormatter stringFromNumber:@123456.7890]); // !12,3460元
文/刘大帅(简书作者)
原文链接:http://www.jianshu.com/p/817029422a72
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

【iOS】NSNumberFormatter的更多相关文章

  1. 【IOS】将一组包含中文的数据按照#ABC...Z✿分组

    上一篇文章[IOS]模仿windowsphone列表索引控件YFMetroListBox里面 我们一步步的实现了WindowsPhone风格的索引. 但是有没有发现,如果你要实现按照字母排序,你还得自 ...

  2. 【IOS】模仿windowsphone列表索引控件YFMetroListBox

    有没有觉得UITableView自带的右侧索引很难用,我一直觉得WindowsPhone中的列表索引非常好用. 所以呢,我们来实现类似Windows Phone中的列表索引(这就是信仰). 最终实现效 ...

  3. 【IOS】自定义可点击的多文本跑马灯YFRollingLabel

    需求 项目中需要用到跑马灯来仅展示一条消息,长度合适则不滚动,过长则循环滚动. 虽然不是我写的,但看了看代码,是在一个UIView里面放入两个UILabel, 在前一个快结束的时候,另一个显示.然而点 ...

  4. 【iOS】在Swift中使用JSONModel

    前言 首先所有的Model还是使用oc来写——看到这一句是不是想关网页了- - #,在swift里面直接写一直报错所以就将就用oc来写了,这里主要是分享一下搭配Alamofire使用的经验. 声明 欢 ...

  5. 【iOS】使用safari对webview进行调试

    [iOS]使用safari对webview进行调试 在web开发的过程中,抓包.调试页面样式.查看请求头是很常用的技巧.其实在iOS开发中,这些技巧也能用(无论是模拟器还是真机),不过我们需要用到ma ...

  6. 【iOS】7.4 定位服务->2.1.1 定位 - 官方框架CoreLocation: 请求用户授权

    本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...

  7. 【iOS】7.4 定位服务->2.1.2 定位 - 官方框架CoreLocation: CLLocationManager(位置管理器)

    本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...

  8. 【iOS】7.4 定位服务->2.1.3.1 定位 - 官方框架CoreLocation 功能1:地理定位

    本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...

  9. 【iOS】7.4 定位服务->2.1.3.2 定位 - 官方框架CoreLocation 功能2:地理编码和反地理编码

    本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...

随机推荐

  1. 一些gcd计数问题

    数论什么的全都忘光了吧QAQ 做了几道简单的题练习一下. bzoj1101: [POI2007]Zap 求有多少对数满足 gcd(x,y)=d, 1<=x<=a, 1<=y<= ...

  2. ViewHolder优化2>:

    ViewHoder优化:     @Override         public View getView(int position, View convertView, ViewGroup par ...

  3. BJITJobs : 北京IT圈高端职位招聘信息,成功率最高的高端求职渠道

    你有实力,但比你差的人都升了,你的师弟都年薪50万了,你还是找不到机会.为什么你离高端机会总是差一步呢?其实你离成功就差一次机会,一个适合你的高端职位的信息! 招聘网站不靠谱:三大网站都是低端职位为主 ...

  4. [LintCode] Trailing Zeroes 末尾零的个数

    Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...

  5. VS2010 中,windows服务不能添加 System.Web 引用

    今天在写windows服务的时候,右键添加引用->.NET中没有找到System.Web,在->Recent中找到System.Web,但添加完后却显示黄色叹号,最后百度一下解决了这个问题 ...

  6. Git生成ssh ksy后进行项目管理

    1.首先你要有一个git账号,然后在网站上Create a New Repository,填好名称后就建立了一个仓库,之后即会出现一些仓库的配置信息... 2.然后你要下载一个git客户端,也可以是m ...

  7. eclipse 下找不到或无法加载主类的解决办法

    有时候 Eclipse 会发神经,好端端的 project 就这么编译不了了,连 Hello World 都会报“找不到或无法加载主类”的错误,我已经遇到好几次了,以前是懒得深究就直接重建projec ...

  8. 通过一组RESTful API暴露CQRS系统功能

    命令和查询责任分离(CQRS)是由Greg Young提出的一种将系统的读(查询).写(命令)操作分离为两种独立子系统的架构模式.命令通常是异步执行的,并存储在一个事务型数据库中,而读操作则通常是最终 ...

  9. ASP.NET MVC 5 Web编程1 -- 入门

    开篇引言 说起ASP.NET MVC,我想作为WebForms开发者第一点要问的是:为什么要使用它?我的理解是:MVC是更细节化的框架,“细节可控”意味着你的系统更精致.具体体现在应用上.MVC的出现 ...

  10. JSP模板继承功能实现

    背景 最近刚入职新公司,浏览一下新公司项目,发现项目中大多数JSP页面都是独立的.完整的页面,因此许多页面都会有如下重复的代码: <%@ page language="java&quo ...