Iphone 目标-c值来自双精度

Iphone 目标-c值来自双精度,iphone,objective-c,ios,double,stringwithformat,Iphone,Objective C,Ios,Double,Stringwithformat,我有以下代码: double d1 = 12.123456789012345; NSString *test1 = [NSString stringWithFormat:@"%f", d1]; // string is: 12.123457 NSString *test1 = [NSString stringWithFormat:@"%g", d1]; // string is: 12.1235 如何获得与d1完全相同的字符串值?如何 NSString *test1 = [NSString

我有以下代码:

double d1 = 12.123456789012345;

NSString *test1 = [NSString stringWithFormat:@"%f", d1]; // string is: 12.123457

NSString *test1 = [NSString stringWithFormat:@"%g", d1]; // string is: 12.1235
如何获得与d1完全相同的字符串值?

如何

NSString *test1 = [NSString stringWithFormat:@"%.15f", d1];
或者干脆选择双重身份

NSString *test1 = [NSString stringWithFormat:@"%lf", d1];

这可能会帮助你看一看苹果的指南

当然,也要仔细阅读


如果确实希望字符串与double精确匹配,则使用
NSString
对其进行编码,并在需要值时调用
doubleValue
。再看看
NSNumberFormatter

@PengOne我为他修正了这个问题,把它转换成了长浮点。@RichardJ.RossIII我的理解是,
%lf
只有在
扫描时才有必要。@PengOne事实上,不,
%f
需要一个32位的浮点数,与文档所说的相反,而
%lf
需要一个64位的浮点数。%lf返回字符串“12.123457”,您所要做的就是指定要在类型说明符之前由.#显示的小数位数。
%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