Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Objective-C中的round()问题_Ios_Objective C_Math - Fatal编程技术网

Ios Objective-C中的round()问题

Ios Objective-C中的round()问题,ios,objective-c,math,Ios,Objective C,Math,试图找出舍入函数在小数点后一百位不起作用的原因 int i=round(0.054545*100); NSLog(@"Rounded value is %d",i); Output: Rounded value is 5 int i=round(0.055*100); NSLog(@"Rounded value is %d",i); Output: Rounded value is 6 任何将0.0545*100~(0.055*100)四舍五入到6的建议。在将其四舍五入到5.45之前,将0.

试图找出舍入函数在小数点后一百位不起作用的原因

int i=round(0.054545*100);
NSLog(@"Rounded value is %d",i);
Output: Rounded value is 5

int i=round(0.055*100);
NSLog(@"Rounded value is %d",i);
Output: Rounded value is 6

任何将0.0545*100~(0.055*100)四舍五入到6的建议。

在将其四舍五入到
5.45
之前,将
0.1
添加到
5.45
中,使其四舍五入到
6

四舍五入函数比您希望输出四舍五入的最低有效位小一个数量级,并使用它进行四舍五入。在这种情况下,
5.45
5.49
相同,
5.40
,因为百分之一数字不重要。如果我没有弄错你的问题,你想把
5.444444
四舍五入到
5
,把
5.44445
四舍五入到
6


我想你要找的是一个递归舍入操作。您需要将
5.45
四舍五入到
5.5
,然后将
5.5
四舍五入到
6
。换句话说,您希望对最小的非零数字进行四舍五入,然后对该输出的最小非零数字进行四舍五入…重复…直到最后一个非零数字是您想要的最低有效数字

规则是什么?总是围捕吗?5.45比6更接近于5,因此有点不清楚您在寻找什么。如果小于5舍入,则向上舍入5或更多。这里是否有问题(除了您为什么不研究
舍入的C定义?)。5.45比6更接近于5,因此向下舍入为5。这是正确的行为。如果您想将5.45舍入到6,请执行
舍入(5.45+0.05)
。(或者加上0.55并使用truncate。)是的,这就是我想要弄明白的。我两次四舍五入就解决了这个问题。第一轮四舍五入=四舍五入(0.054545*1000);secondRoundRounding=四舍五入([[NSNumber numberWithInt:firstRoundRounding]floatValue]/10);在一个欠你5.45美元但只有单身的朋友身上测试一下。看看你能不能让他给你6美元。