要加倍的字符串

问题描述:

我希望你能帮助我解决这个“小问题”。我想将一个字符串转换为double/float。要加倍的字符串

NSString *stringValue = @"1235"; 
priceLabel.text = [NSString stringWithFormat:@"%d",[stringValue doubleValue]/(double)100.00]; 

我希望这对priceLabel设置为12,35,但我得到一些奇怪的长字符串的意思与我无关。

我曾尝试:

priceLabel.text = [NSString stringWithFormat:@"%d",[stringValue intValue]/(double)100.00]; 

priceLabel.text = [NSString stringWithFormat:@"%d",[stringValue doubleValue]/100]; 

但都没有成功。

+0

结帐这个问题:http://stackoverflow.com/questions/169925/how-to-do-string-conversions-in-objective- c ...您需要为用户在其设置中设置的语言环境设置一个“NSNumberFormatter”,并使用它来获取字符串中的信息。 – klaustopher

您必须使用%f来显示float/double值。

然后%.2f点后意味着2digits

NSString *stringValue = @"1235"; 

NSString *str = [NSString stringWithFormat:@"%.2f",[stringValue doubleValue]/(double)100.00]; 

NSLog(@"str : %@ \n\n",s); 

priceLabel.text = str; 

OUTPUT:

STR:12.35

+0

谢谢分配!这对我有效! –

+0

然后你可以通过勾选mark.thanks来标记这个答案。 –

+0

即时帮助http://chat.stackoverflow.com/rooms/682/conversation/do-u-want-instant-help-for-ur-question-or-ru-new-bee-to-iphone-ipad-开发 –

我想你错了格式字符串。当您有:

[NSString stringWithFormat:@"%d", ...]; 

你要真有:

[NSString stringWithFormat:@"%f", ...]; 

%d用于整数值。但是你试图显示一个浮点数(%f)。

这是如何转换NSStringdouble

double myDouble = [myString doubleValue]; 
+2

奇怪的问题是字符串翻倍。接受的答案甚至没有解决这个问题。但是这里有一个。 :) 谢谢 – Houman