码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-01-27 21:35:16      阅读:155      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!