首页 > 其他 > 详细

对浮点数进行四舍五入处理的函数

时间:2015-01-27 21:33:11      阅读:196      评论:0      收藏:0      [点我收藏+]

英语不好, 见谅。

 1 float myRound (float src, int idx)
 2 {
 3     //move idx numbers right of the point
 4     //to the left
 5     for (int i = idx; i--;) {
 6         src *= 10;
 7     }
 8 
 9     //get left part of src
10     float dest = static_cast<int>(src);
11     
12     //if the number behind the idx
13     //are larger than 5, then article idx plus 1
14     if (src >= dest + 0.5) {
15         dest += 1;
16     }
17 
18     //right nuber back to home
19     for (int i = idx; i--;) {
20         dest /= 10;
21     }
22     return std::move (dest);
23 }

 

对浮点数进行四舍五入处理的函数

原文:http://www.cnblogs.com/wuOverflow/p/4253886.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!