PNCurrency可以轻松地在您的应用中转换不同的货币格式。如果您之前曾经处理过应用中的货币,那么您知道在分、双精度浮点数、字符串甚至这些格式的某些格式化变体之间进行转换是一个非常常见的任务。这正是PNCurrency的用武之地,它提供了一个简单的接口来完成这些操作。请继续阅读以了解接口和如何开始。
您可以使用git克隆仓库
git clone https://github.com/pinnrepo/PNCurrency.git
然后将文件添加到您的项目中,并使用本地导入头
#import "PNCurrency.h"
以下是使用PNCurrency替代自己编写的理由:
以下是快速了解如何开始使用PNCurrency的方法:
有几种方法可以创建一个新的PNCurrency对象,以下是初始化对象的最佳方法。
普通init将金额默认为零。按如下方式分配初始化对象
PNCurrency *currency = [[PNCurrency alloc] init];
当您有一个以分表示的金额时,请使用此方法来创建对象
// Initialize currency to a dollar
PNCurrency *currency = [[PNCurrency alloc] initWithCentsAmount:100];
您可能有一个原始双精度浮点数,在这种情况下,您将希望使用此方法
// Initialize currency to a dollar
PNCurrency *currency = [[PNCurrency alloc] initWithDoubleAmount:1.00];
最后,如果您有一个表示可以喂食的金额的字符串,您也可以输入它
// Initialize currency to a dollar
PNCurrency *currency = [[PNCurrency alloc] initWithStringAmount:@"1.00"];
很可能您需要对对象的生命周期中的货币值进行更新,这可以通过几种不同的简单方式进行
您可以通过更改centsAmount属性来修改金额
PNCurrency *currency = [[PNCurrency alloc] init];
currency.centsAmount = 10 // value is now $0.10
或者用新的双精度值来更改它
PNCurrency *currency = [[PNCurrency alloc] init];
[currency setWithDoubleAmount:1.11] // value is now $1.11
最后,还可以用新的字符串值来更改它
PNCurrency *currency = [[PNCurrency alloc] init];
[currency setWithStringAmount:@"2.00"] // value is now $2.00
这里没有太多花哨的东西,但是有安全便捷的自动转换,当您实际需要返回值时,这里是如何操作的
您可以利用cents amount属性来获取原始的金额(以未签名整数字符串形式表示)
PNCurrency *currency = [[PNCurrency alloc] initWithStringAmount:@"1.00"];
[currency centsAmounts] // Value is 100 (you can also use currency.centsAmount
以双精度值返回金额,可能会截断尾随零
PNCurrency *currency = [[PNCurrency alloc] initWithCentsAmount:100];
[currency doubleAmount] // Value is 1.00
恰如期望地以字符串形式返回金额
PNCurrency *currency = [[PNCurrency alloc] initWithCentsAmount:100];
NSLog(@"%@", [currency stringAmount]) // prints "1.00"
通常您会希望将这些内容打印到屏幕上,所以您可能会想要类似"$1.00"的东西,而不是双精度或以分为单位的金额。这些都是方便的格式化工具。
直接打印带有美元符号的值
PNCurrency *currency = [[PNCurrency alloc] initWithCentsAmount:100];
NSLog(@"%@", [currency formattedAmount]) // prints "$1.00"
在美元符号和金额本身之间打印空格
PNCurrency *currency = [[PNCurrency alloc] initWithCentsAmount:100];
NSLog(@"%@", [currency formattedAmountWithSpace]) // prints "$ 1.00"
您可以在这里查看完整的参考文档。
以下是一些即将推出功能和改进的列表
git checkout -b my-new-feature
git commit -am '添加一些功能'
git push origin my-new-feature
flake8 . --exclude=.venv
MIT 许可证 (MIT)
版权所有 (c) 2016 Pinn Technologies, Inc.
在此授予任何获得此软件及相关文档资料副本(以下简称“软件”)的个人免费使用该软件的权利,不受限制地处理该软件,包括但不限于以下权利:使用、复制、修改、合并、发布、分发、许可和/或出售软件副本,以及准许将软件提供给他人以便其使用,前提是必须遵守以下条件:
必须在所有副本或软件的主要部分中包含上述版权声明和本许可声明。
本软件按原样提供,不提供任何明示或暗示的担保,包括但不限于适销性、特定目的的适用性和非侵权性。在任何情况下,作者或版权所有者不应对任何索赔、损害或其他责任负责,无论这种责任是基于合同、侵权或其他方式,源自、因之而产生或与之相关本软件或其使用或处理。