Represent double value as NSString
What is the correct way of represent double value as NSString ? In my code, it is calculating or rather doing some kind of rounding..I am not sure. Below is my code;
X = [NSString stringWithFormat:@"%d", [abc doubleValue] - [xyz doubleValue]];
I am getting abc is 69206000000, xyz is 31687000000, X is 5872开发者_开发技巧0256
Please help me.
Use the %f
format modifier to display a double value - the %d
in your code refers to an integer value. (See String Format Specifiers for more info.)
Use %lf
for double value
X = [NSString stringWithFormat:@"%lf", [abc doubleValue] - [xyz doubleValue]];
Hope this will help you.
%d is for integers so you'll get some funky output passing in a double or float. Try this instead:
NSString *str = [NSString stringWithFormat:@"%f", [abc doubleValue] - [xyz doubleValue]];
%d means integer. Use %f. Read up on format specifiers.
精彩评论