如何在Objective-C中进行字符串转换?

时间:2008-10-04 07:36:01

标签: objective-c cocoa cocoa-touch

我想将一个字符串转换为double,然后对其进行一些数学运算,将其转换回字符串。

如何在Objective-C中执行此操作?

有没有办法将double舍入到最接近的整数?

12 个答案:

答案 0 :(得分:228)

您可以使用

将NSString转换为double
double myDouble = [myString doubleValue];

可以将<舍入到最近的int作为

int myInt = (int)(myDouble + (myDouble>0 ? 0.5 : -0.5))

老实说,我不确定是否有更简化的方式转换回字符串而不是

NSString* myNewString = [NSString stringWithFormat:@"%d", myInt];

答案 1 :(得分:70)

要真正正确地从字符串转换为数字,您需要使用为您正在读取字符串的语言环境配置的NSNumberFormatter实例。

不同的区域设置会以不同方式格式化数字。例如,在世界的某些地方,COMMA用作小数分隔符,而在其他地方则为PERIOD - 千位分隔符(使用时)被反转。除非它是一个空间。或根本不存在。

这实际上取决于输入的来源。最安全的做法是为输入格式化方式配置NSNumberFormatter,并使用-[NSFormatter numberFromString:]从中获取NSNumber。如果您想处理转化错误,可以改用-[NSFormatter getObjectValue:forString:range:error:]

答案 2 :(得分:32)

添加到olliej的答案,您可以使用NSNumber stringValue

[[NSNumber numberWithInt:myInt] stringValue]
stringValue上的{p> NSNumber调用descriptionWithLocale:nil,为您提供值的本地化字符串表示形式。我不确定[NSString stringWithFormat:@"%d",myInt]是否会为您提供myInt的正确本地化代表。

答案 3 :(得分:7)

olliej的rounding方法对于否定数字是错误的

  • 2.4 round是2(olliej的方法是正确的)
  • -2.4舍入为-2(olliej的方法返回-1)

这是另一种选择

  int myInt = (int)(myDouble + (myDouble>0 ? 0.5 : -0.5))

你当然可以使用math.h的舍入函数

答案 4 :(得分:7)

这是NSNumberFormatter读取本地化数字String(xCode 3.2.4,osX 10.6)的工作示例,以便为其他人节省我刚刚花费的时间。注意:虽然它可以处理诸如“8,765.4”之类的尾随空白,但它无法处理前导空格,并且无法处理杂散文本字符。 (错误的输入字符串:“8”和“8q”和“8 q”。)

NSString *tempStr = @"8,765.4";  
     // localization allows other thousands separators, also.
NSNumberFormatter * myNumFormatter = [[NSNumberFormatter alloc] init];
[myNumFormatter setLocale:[NSLocale currentLocale]]; // happen by default?
[myNumFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
     // next line is very important!
[myNumFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; // crucial

NSNumber *tempNum = [myNumFormatter numberFromString:tempStr];
NSLog(@"string '%@' gives NSNumber '%@' with intValue '%i'", 
    tempStr, tempNum, [tempNum intValue]);
[myNumFormatter release];  // good citizen

答案 5 :(得分:3)

// Converting String in to Double

double doubleValue = [yourString doubleValue];

// Converting Double in to String
NSString *yourString = [NSString stringWithFormat:@"%.20f", doubleValue];
// .20f takes the value up to 20 position after decimal

// Converting double to int

int intValue = (int) doubleValue;
or
int intValue = [yourString intValue];

答案 6 :(得分:2)

对于舍入,您应该使用math.h中定义的C函数。

int roundedX = round(x);

按住Option键并双击Xcode中的round,它将显示包含各种函数的手册页,用于舍入不同类型。

答案 7 :(得分:2)

对于从数字到字符串的转换,如何使用新的文字语法(XCode&gt; = 4.4),它更紧凑。

int myInt = (int)round( [@"1.6" floatValue] );

NSString* myString = [@(myInt) description];

(将其装箱为NSNumber并使用NSObjects' description method转换为字符串

答案 8 :(得分:1)

这是我所知道的最简单的方法:

float myFloat = 5.3;
NSInteger myInt = (NSInteger)myFloat;

答案 9 :(得分:1)

从这个示例中,您可以通过两种方式查看转化:

NSString *str=@"5678901234567890";

long long verylong;
NSRange range;
range.length = 15;
range.location = 0;

[[NSScanner scannerWithString:[str substringWithRange:range]] scanLongLong:&verylong];

NSLog(@"long long value %lld",verylong);

答案 10 :(得分:1)

我最终使用了这个方便的宏:

#define STRING(value)               [@(value) stringValue]

答案 11 :(得分:1)

将在textfield中输入的文本转换为整数

double mydouble=[_myTextfield.text doubleValue];

四舍五入到最近的双倍

mydouble=(round(mydouble));

舍入到最近的int(仅考虑正值)

int myint=(int)(mydouble);

从double转换为字符串

myLabel.text=[NSString stringWithFormat:@"%f",mydouble];

NSString *mystring=[NSString stringWithFormat:@"%f",mydouble];

从int转换为字符串

myLabel.text=[NSString stringWithFormat:@"%d",myint];

NSString *mystring=[NSString stringWithFormat:@"%f",mydouble];