iOS round a float

前端 未结 2 1498
离开以前
离开以前 2021-02-18 19:43

I have a floating point number that have more decimal digits, for example:

float fRes = 10.0 / 3.0;

actually the fRes value is 3.3333333333333

2条回答
  •  不思量自难忘°
    2021-02-18 20:10

    Assuming that you're looking for the correct function to round to a certain number of digits, you'll probably find it easiest to do the following:

    fResOk = roundf( fRes*100.0)/100.0;
    

    That will multiply the value by 100 (giving you your 2 digits of significance), round the value, and then reduce it back to the magnitude you originally started with.

提交回复
热议问题