开发者

String to double

开发者 https://www.devze.com 2023-04-07 08:35 出处:网络
I hope you can help me 开发者_运维问答out with this \'small\' problem. I want to convert a string to a double/float.

I hope you can help me 开发者_运维问答out with this 'small' problem. I want to convert a string to a double/float.

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

I was hoping this to set the priceLabel to 12,35 but I get some weird long string meaning nothing to me.

I have tried:

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

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

but all without success.


This is how to convert an NSString to a double

double myDouble = [myString doubleValue];


You have to use %f to show float/double value.

then %.2f means 2digits after dot

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


I think you have the wrong format string. Where you have:

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

You should really have:

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

%d is used for integer values. But you're trying to display a floating point number (%f).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号