C++ 自己实现四舍五入函数
来源:互联网 发布:天府广场美食攻略 知乎 编辑:程序博客网 时间:2024/05/18 00:49
本文仅仅发布在CSDN我的博客:点击打开链接。
http://blog.csdn.net/pythontojava?viewmode=contents
转载请注明出处。
我用的IDE是VS2013.。代码在vc++6.0不能编译,要把int _tmain(int argc, _TCHAR* argv[]) 改成 int main() 。
1.float与double的四舍五入
#include<iostream>#include <math.h> using namespace std;float _45(float value, int n)//float类型的四舍五入函数 _45(float 值,第几位){float temp = value* pow(10, -n);//当n<0,程序表示精确到小数点后第|n|位temp = floor(temp + 0.5);//当n=0,表示四舍五入只保留数的整数部分return (temp * pow(10, n));}double _45(double value, int n)// double类型的四舍五入函数,_45(float 值,第几位){double temp = value* pow(10, -n);//当n<0,程序表示精确到小数点后第|n|位temp = floor(temp + 0.5);//当n=0,表示四舍五入只保留数的整数部分return (temp * pow(10, n));}int _tmain(int argc, _TCHAR* argv[]){float a = 158.55;float b = 111.44;cout <<a<<"->"<<_45(a,-1)<<endl;//小数点后一位cout << b<< "->" << _45(b,-1);cout << endl;system("pause");return 0;}
==========分割线==========
用函数模板实现
#include "stdafx.h"#include<iostream>#include <math.h> using namespace std;template<typename T>T _45(T x, int n){T temp = x * pow(10, -n);temp = floor(temp + 0.5);return (temp * pow(10, n));}int _tmain(int argc, _TCHAR* argv[]){float a = 158.55;double b = 211.13;cout <<_45<float>(a,-1)<<endl;cout << _45(b, -1) << endl;system("pause");return 0;}
0 0
- C++ 自己实现四舍五入函数
- C语言四舍五入函数
- C语言四舍五入函数
- C语言实现四舍五入
- 用round 函数 实现 四舍五入
- 用round 函数 实现 四舍五入
- JS math函数实现四舍五入
- C/C++ 四舍五入的实现
- C++(C)数字四舍五入函数
- c --- 自编写四舍五入的myround函数
- C语言测试。自己实现scandir 函数
- c/c++ strcpy函数的自己实现
- c/c++自己实现strstr函数
- C/C++实现正负数四舍五入
- C语言四舍五入的实现方法。
- C语言:实现数据的四舍五入
- 四舍五入函数
- 四舍五入函数
- MFC绘图(转载)
- 陈怡暖:非农好坏参半,金银延续盘整(午评)
- 不甘平凡的传智之旅
- HDU 1102 Constructing Roads
- 原码、反码、补码
- C++ 自己实现四舍五入函数
- Dialog在android中的应用(二)
- 第五章第九题
- oracle两时间相减得到相差的时间
- 《硬骨头》沈阳热拍 吴其江耍酒疯调戏美女
- Android的ProgressBar样式||||||style="?android:attr/progressBarStyleSmall"|||系统预置定义样式的用法是?android:attr/x
- 如何加载storyboard中的控制器?
- 内置系统账户:Local system/Network service/Local Service 区别
- java语法基础