NumberFormatter将NSNumber转换为NSStr

NumberFormatter将NSNumber转换为NSStr

本文介绍了使用NSNumberFormatter将NSNumber转换为NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 NSNumber 转换为 NSString ,假设在此示例中,theindex 1000000

  NSNumber * firstNumber = [tempDict objectForKey:@theindex]; 
NSString * convertNumber = [firstNumber stringValue];

返回 NSString 1000000 p>

我想将字符串的值设置为1,000,000。



我不关心本地化,但从其他问题了解,应该实现 NSNumberFormatter

解决方案

下面是使用NSNumberFormatter的标准格式化风格的示例:

  NSNumber * firstNumber = [NSNumber numberWithInt:123456789]; 
NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString * convertNumber = [formatter stringForObjectValue:firstNumber];
NSLog(@value:%@,convertNumber);

您可以参考的其他示例:


I am converting an NSNumber to an NSString assuming in this example that the selected key in "theindex" is 1000000

NSNumber  *firstNumber = [tempDict objectForKey:@"theindex"];
NSString *convertNumber = [firstNumber stringValue];

Returning the NSString "1000000"

I would like the string's value to be "1,000,000".

I am not concerned with localization, but understand from other questions that NSNumberFormatter should be implemented. I am not sure how to accomplish this?

解决方案

Here is an example using a standard formatting style of NSNumberFormatter:

NSNumber *firstNumber = [NSNumber numberWithInt:123456789];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *convertNumber = [formatter stringForObjectValue:firstNumber];
NSLog(@"value : %@", convertNumber);

Other examples you can reference at:
http://iosdevelopertips.com/cocoa/formatting-numbers-nsnumberformatter-examples.html

这篇关于使用NSNumberFormatter将NSNumber转换为NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:28