Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
为什么NSLog(@"%0.0f", 1.0/10.0);输出0?
NSLog(@"%0.0f", 1.0/10.0);
不是1.0/10.0两个双值的除法吗?为什么四舍五入到0?
1.0/10.0
因为您要求它使用格式说明符打印 0 位小数。
NSLog(@"%f", 1.0/10.0);
0.100000
printf 规格:
一个可选的精度,它给出 ... 出现在 a、A、e、E、f 和 F 转换说明符的基数字符 [在这种情况下,输出中的小数点 - JC] 之后出现的位数。 . 精度采用句点 ('.') 后跟星号 ('*') 的形式,如下所述,或可选的十进制数字字符串,其中空数字字符串被视为零。