2011-07-06 35 views
0

如何正确地将NSString转换为xcode 3.2中的双精度型?这是我的代码(缩写),但是当我运行它时,消息说(空)。我究竟做错了什么?非常感谢!字符串双击疑难解答

@synthesize class1sem1; 

-(IBAction)buttonPressed 
{ 
NSString *myString = class1sem1.text; 
double myDouble = [myString doubleValue]; 


NSString *message = [[NSString alloc] initWithFormat: 
        @"%@", myDouble]; 

UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Result" message:message delegate:nil cancelButtonTitle:@"Great!" otherButtonTitles:nil]; 
[alert show]; 
[alert release]; 
[message release]; 

}

回答

1

使用%G为你的双..不是%@

NSString *message = [[NSString alloc] initWithFormat: 
       @"%g", myDouble]; 
+0

谢谢!这样简单的解决方案! –

1

试图显示双就好像它是一个对象。使用此来代替:

NSString *message = [[NSString alloc] initWithFormat: 
         @"%f", myDouble]; 
        //^note 'f' instead of '@' 

f是指 “双精度浮点数”,而@指 “Objective-C的对象”。 A double是一个双精度浮点数,而不是Objective-C对象。