gpt4 book ai didi

iphone - double 中的 objective-c stringvalue

转载 作者:可可西里 更新时间:2023-11-01 03:26:30 25 4
gpt4 key购买 nike

我有以下代码:

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完全相同的字符串值?

最佳答案

Apple 的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

另请阅读 floating point (in)accuracy ,当然还有 What Every Computer Scientist Should Know About Floating-Point Arithmetic .

如果您真的希望字符串与 double 匹配,则使用 NSString 对其进行编码,并在需要该值时调用 doubleValue。另请查看 NSNumberFormatter

关于iphone - double 中的 objective-c stringvalue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8495076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com