最简单的方法是将NSString转换为其在Cocoa中的货币等价物 [英] Easiest way to convert a NSString to its currency equivalent in Cocoa

查看:64
本文介绍了最简单的方法是将NSString转换为其在Cocoa中的货币等价物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSString值@78000。

I have a NSString value of @"78000". How do I get this in currency format, i.e. $78,000 with it remaining an NSString.

推荐答案

您需要使用数字格式化程序。注意,这也是如何以正确的格式显示用户区域设置的日期/时间等

You need to use a number formatter. Note this is also how you would display dates/times etc in the correct format for the users locale

// alloc formatter
NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init];

// set options.
[currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4];
[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];

NSNumber *amount = [NSNumber numberWithInteger:78000];

// get formatted string
NSString* formatted = [currencyStyle stringFromNumber:amount]

[currencyStyle release];

这篇关于最简单的方法是将NSString转换为其在Cocoa中的货币等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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