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

英语不好, 见谅。

 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 }

 

posted @ 2015-01-27 20:26  wu_overflow  阅读(565)  评论(0编辑  收藏  举报