iphone – 字符串加倍

前端之家收集整理的这篇文章主要介绍了iphone – 字符串加倍前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我希望你能帮我解决这个“小问题”.我想将一个字符串转换为double / float.

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

我希望这可以将价格标准设置为12,35,但我得到一些奇怪的长字符串对我来说没有任何意义.

我试过了:

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

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

但都没有成功.

解决方法

您必须使用%f来显示浮点/双精度值.

然后%.2f表示点后2位数

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

猜你在找的Xcode相关文章