要加倍的字符串
我希望你能帮助我解决这个“小问题”。我想将一个字符串转换为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];
但都没有成功。
您必须使用%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
谢谢分配!这对我有效! –
然后你可以通过勾选mark.thanks来标记这个答案。 –
即时帮助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
)。
这是如何转换NSString
为double
double myDouble = [myString doubleValue];
奇怪的问题是字符串翻倍。接受的答案甚至没有解决这个问题。但是这里有一个。 :) 谢谢 – Houman
结帐这个问题:http://stackoverflow.com/questions/169925/how-to-do-string-conversions-in-objective- c ...您需要为用户在其设置中设置的语言环境设置一个“NSNumberFormatter”,并使用它来获取字符串中的信息。 – klaustopher