Objective-C float 被限制为小数点后 6 位?

标签 objective-c c

<分区>

我有一个 Objective-C 项目需要显示像 0.00000217 这样的数字,以及非常小的数字。问题是,Objective-C 将其四舍五入到小数点后 6 位,因此它显示为 0.000002。有没有显示更多小数位的类型?我的代码:

float floatValueOfSmallNumber = [value floatValue];
[theLabel setText:[NSString stringWithFormat:@"%f", floatValueOfSmallNumber]];

谢谢!

最佳答案

虽然 float 只有大约 7 位有效的小数位,但这不是您在这里遇到的问题;毕竟,0.00000217 只有三位有效数字。

您正在使用从 C 继承并定义的 %f 格式说明符(7.21.6 格式化输入/输出函数):

A double argument representing a floating-point number is converted to decimal notation in the style [−]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is zero and the # flag is not specified, no decimal-point character appears. If a decimal-point character appears, at least one digit appears before it. The value is rounded to the appropriate number of digits.

使用 double 不会改变这个;相反,您需要更改格式说明符。如果您不介意科学记数法,可以使用 %e%g,但另一种选择是使用精度说明符:%.10f,例如,将打印十位十进制数字。

关于Objective-C float 被限制为小数点后 6 位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21395557/

相关文章:

c - 可变函数参数的段错误

ios - 如何插入新字体并强制更改 UIButton?

ios - 在 if 条件下将 NSArray 变成 NSMutableArray

ios - 如何将 NSMutableArray 添加到 NSMutableArray Objective-c

objective-c - iOS:如何干净地删除 UIButton socket ?

c - 覆盖 gcov 函数以获取已执行的代码行

objective-c - "Block"主线程 (dispatch_get_main_queue()) 和(或不)定期运行 currentRunLoop - 有什么区别?

c - 如何在linux中控制dcd

无法在 opencl 中获取我的显卡设备

c - 为什么free不释放内存?