objective-c stringvalue from double

大憨熊 提交于 2019-12-05 04:47:22

It may help you to take a look at Apple's guide to String Format Specifiers.

%f  64-bit floating-point number 
%g  64-bit floating-point number (double), printed in the style of %e if the exponent is less than –4 or greater than or equal to the precision, in the style of %f otherwise

Also read up on floating point (in)accuracy, and, of course What Every Computer Scientist Should Know About Floating-Point Arithmetic.

If you really want the string to match the double exactly, then use NSString to encode it and call doubleValue when you want the value. Also take a look at NSNumberFormatter.

A Salcedo

How about

NSString *test1 = [NSString stringWithFormat:@"%.15f", d1];

Or simply go for the double as

NSString *test1 = [NSString stringWithFormat:@"%lf", d1];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!