使用 NSNumberFormatter 在货币符号和值之间填充空格
如果这是一个愚蠢的问题,我深表歉意,但我正在尝试为我的 iPhone 应用程序格式化货币值,并且正在努力左对齐货币符号,但右对齐该值。因此,“$123.45”的格式为(例如)
$ 123.45depending on format-width. This is a kind of accounting format (I think).
我已经尝试了 NSNumberFormatter 的各种方法,但无法得到我需要的。
谁能建议如何做到这一点?
谢谢菲托
Apologies if this is a dumb question but I'm trying to format a currency value for my iphone app and am struggling to left-justify the currency symbol, but right-justify the value. So, "$123.45" is formatted as (say)
$ 123.45
depending on format-width. This is a kind of accounting format (I think).
I've tried various methods with NSNumberFormatter but can't get what I need.
Can anyone advise on how to do this?
Thanks
Fitto
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找
NSNumberFormatter
的paddingPosition
属性。您需要将其设置为NSNumberFormatterPadAfterPrefix
以获得所需的格式。You're looking for the
paddingPosition
property ofNSNumberFormatter
. You need to set this toNSNumberFormatterPadAfterPrefix
for the desired format.这对我不起作用。通过这样做,我可以在货币符号和金额之间添加空格。
Swift 3.0
完整代码:
此扩展位于以 MinorUnits 表示金额的 Int 上。即美元用2位数字表示,而日元则不用数字表示。这就是此扩展将返回的内容:
千位和小数分隔符由用户区域设置确定。
This didn't work for me. I was able to add a space between the currency symbol and the amount by doing this.
Swift 3.0
Complete code:
This extension is on an Int that expresses the amount in MinorUnits. I.e. USD is expressed with 2 digits, while japanese yen is expressed without digits. So this is what this extension would return:
The thousand and decimal separator are determined by the users locale.