我有一個NSString值@「78000」。我如何以貨幣格式獲得這個數據,即$ 78,000,並保留一個NSString。將NSString轉換爲Cocoa等價貨幣的最簡單方法
12
A
回答
21
您需要使用數字格式器。注意:這也是你將如何顯示日期/時間等以正確的格式,爲用戶區域設置
// 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];
3
3
答案上面會轉換爲數字,但這裏是真正轉換的NSString貨幣格式的方法
- (NSString*) formatCurrencyWithString: (NSString *) string
{
// alloc formatter
NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init];
// set options.
[currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4];
// reset style to no style for converting string to number.
[currencyStyle setNumberStyle:NSNumberFormatterNoStyle];
//create number from string
NSNumber * balance = [currencyStyle numberFromString:string];
//now set to currency format
[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];
// get formatted string
NSString* formatted = [currencyStyle stringFromNumber:balance];
//release
[currencyStyle release];
//return formatted string
return formatted;
}
相關問題
- 1. 將NSString轉換爲貨幣格式
- 2. 轉換貨幣價值
- 3. Knockout Circular Reference(簡單貨幣轉換器)
- 4. 將NSString轉換爲貨幣 - 完整的故事
- 5. 在HTML中的貨幣價格轉換
- 6. 自動將網頁上的價格轉換爲其他貨幣
- 7. 轉換貨幣
- 8. 將貨幣/貨幣轉換爲Cent,反之亦然
- 9. 有沒有簡單的方法可以將MySQL錶轉換爲Redis等價物?
- 10. 轉換爲貨幣格式
- 11. 將int轉換爲貨幣格式
- 12. 將貨幣$ 232,680.00轉換爲十進制
- 13. 如何將int轉換爲貨幣?
- 14. 將NVARCHAR轉換爲貨幣值
- 15. SQL將varchar轉換爲貨幣
- 16. PHP將數字轉換爲貨幣
- 17. 將C#貨幣轉換爲字符串
- 18. FoxPro將貨幣轉換爲數字
- 19. 將貨幣格式轉換爲數字
- 20. 如何將貨幣轉換爲數字?
- 21. 將整數轉換爲貨幣
- 22. 如何將貨幣轉換爲Decimal vb.net?
- 23. 貨幣轉換器
- 24. Django貨幣轉換
- 25. Javascript貨幣轉換
- 26. 在Grails中代表貨幣(價值+貨幣)的最佳實踐
- 27. 將unicode代碼轉換爲UTF-8的最簡單方法
- 28. 將EJB 2.0項目轉換爲EJB 3.0的最簡單方法
- 29. 將xml轉換爲html最簡單的方法是什麼?
- 30. 將列表轉換爲Java集合的最簡單方法