iOS围绕一个浮动 [英] iOS round a float

查看:222
本文介绍了iOS围绕一个浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  float fRes = 10.0 / 3.0;我有一个浮点数有更多的小数位,例如: 

实际上fRes的值是3.3333333333333
它可能被设置为例子2的十进制数字: p>

  float fRes = 10.0 / 3.0; 
// fRes是3.333333333333333333333333
float fResOk = FuncRound(fRes,2);
// fResOk是3.33

提前致谢
<假设你正在寻找正确的函数来四舍五入到一定数量的数字,你可能会发现最容易做到以下几点:

  fResOk = roundf(fRes * 100.0)/100.0; 

这将乘以 100 (给你你的2位数的重要性),四舍五入的价值,然后减少它回到您最初开始的数量级。

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 it's possible set for example 2 decimal digits:

float fRes = 10.0 / 3.0;
// fRes is 3.333333333333333333333333
float fResOk = FuncRound( fRes, 2 );
// fResOk is 3.33

thanks in advance

解决方案

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.

这篇关于iOS围绕一个浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆