String to double
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
).
精彩评论