Objective-C:如何将字符串格式化为 $ Price [英] Objective-C: How to format string as $ Price

查看:26
本文介绍了Objective-C:如何将字符串格式化为 $ Price的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它们是一种将字符串格式化为 $ 价格的内置方式,例如12345.45 转换为 $12,345.45?

Is their a built-in way of formatting string as $ price, e.g. 12345.45 converted to $12,345.45?

推荐答案

假设您使用的是 Cocoa(或只是 Foundation),您可以使用 NSNumberFormatter 并将其样式设置为货币:

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];

默认情况下,它使用您系统的区域设置,但您可以更改它以及许多其他属性,请参阅 NSNumberFormatter API 文档.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆