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

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

问题描述

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

解决方案<假设你正在使用Cocoa(或者只是Foundation),你可以使用NSNumberFormatter并将其样式设置为currency:

  NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init]; 
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
... = [formatter stringFromNumber:number];

默认情况下,它使用系统的语言环境,但是可以更改其他属性,请参阅 NSNumberFormatter 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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