本文介绍了Objective-C:如何将字符串格式化为$ Price的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
它们是将字符串格式化为$ price的内置方式,例如 12345.45
转换为 $ 12,345.45
?
解决方案
NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
... = [formatter stringFromNumber:number];
默认情况下,它使用系统的语言环境,但是可以更改其他属性,请参阅 API文档。
Is their a built-in way of formatting string as $ price, e.g. 12345.45
converted to $12,345.45
?
解决方案
Assuming you are using Cocoa (or just Foundation), you can use NSNumberFormatter and set its style to currency:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
... = [formatter stringFromNumber:number];
By default it uses the locale of your system, but you can change that and lots of other properties, see the NSNumberFormatter API docs.
这篇关于Objective-C:如何将字符串格式化为$ Price的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-19 00:54