Objective-C: floorf( ) returns wrong value

柔情痞子 提交于 2019-12-06 08:23:58

Many decimal floating point fractions cannot be represented as exact fractions in binary, so they have to be approximated. 2.953 is being approximated as something like 2.95299999. When you multiply by 1000, the result is 2952.99999, and when you get the floor of this, it's 2952.

To solve this, you can either use round() instead of ffloorf(), or you can add 0.5 before calling ffloorf():

float withNoFractions = floorf(passedPrice * placed + 0.5);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!