若只需要得到四舍五入的输出,那我们可以利用printf来进行输出,选择保留小数点后0位,即可达到四舍五入的目的
- #include
-
- using namespace std;
-
- int main()
- {
- double a = 1.4999999;
- double b = 1.5000001;
- double n_a = -1.4999999;
- double n_b = -1.5000001;
-
- printf("%.0f\n", a); // 1
- printf("%.0f\n", b); // 2
- printf("%.0f\n", n_a); // -1
- printf("%.0f\n", n_b); // -2
- return 0;
- }
只需要写一个函数判断当前数减去其整数部分是否大于等于0.5,若是则其整数部分进1,否直接输出其整数部分
- #include
-
- using namespace std;
-
- // 用于四舍五入
- int round_0 (double n)
- {
- // 若为负数,则先化为正数再进行四舍五入
- if (n > 0)
- return n - int(n) >= 0.5 ? int(n)+1 : int(n);
- else
- return -n - int(-n) >= 0.5 ? -(int(-n) + 1) : -int(-n);
- }
-
- int main()
- {
- double a = 1.4999999;
- double b = 1.5000001;
- double n_a = -1.4999999;
- double n_b = -1.5000001;
- cout << round_0(a) << endl; // 1
- cout << round_0(b) << endl; // 2
- cout << round_0(n_a) << endl; // -1
- cout << round_0(n_b) << endl; // -2
- return 0;
- }
头文件
- #include
- #include
-
- using namespace std;
-
- int main()
- {
- double a = 1.4999999;
- double b = 1.5000001;
- double n_a = -1.4999999;
- double n_b = -1.5000001;
- cout << round(a) << endl; // 1
- cout << round(b) << endl; // 2
- cout << round(n_a) << endl; // -1
- cout << round(n_b) << endl; // -2
- return 0;
- }
类似的,我们可以利用printf函数来完成自动的四舍五入过程。
若保留两位小数,那么我们可以直接使用printf输入小数点后两位即可。注意如果输出结果的0不够小数点后两位,会自动补齐到小数点后两位。
- #include
-
- using namespace std;
-
- int main()
- {
- double a = 1.499;
- double b = 1.500;
- double c = 1.48;
- double d = 1.5;
- double n_a = -1.499;
- double n_b = -1.500;
-
- printf("%.2f\n", a); // 1.50
- printf("%.2f\n", b); // 1.50
- printf("%.2f\n", c); // 1.48
- printf("%.2f\n", d); // 1.50
- printf("%.2f\n", n_a); // -1.50
- printf("%.2f\n", n_b); // -1.50
- return 0;
- }
setpricision()接受一个整型参数,表示保留多少位有效数字;fixed(std::fixed)表示用于setpricision的有效数字针对的是小数点后的有效数字。这两个函数都是作用于cout, cin之类的IO流
- #include
- #include
-
- using namespace std;
-
- int main()
- {
- double a = 1.499;
- double b = 1.500;
- double c = 1.48;
- double d = 1.5;
- double n_a = -1.499;
- double n_b = -1.500;
-
- cout << fixed << setprecision(2) << a << endl; // 1.50
- cout << fixed << setprecision(2) << b << endl; // 1.50
- cout << fixed << setprecision(2) << c << endl; // 1.48
- cout << fixed << setprecision(2) << d << endl; // 1.50
- cout << fixed << setprecision(2) << n_a << endl; // -1.50
- cout << fixed << setprecision(2) << n_b << endl; // -1/50
- return 0;
- }
要得到四舍五入小数点后的结果,我们可以将小数转换为整数来处理,然后再转换为小数。例如我们需要将1.2345这个数保留小数点后两位四舍五入,我们可以先将其乘以100转换为123.45,然后套用整数的方法来进行四舍五入,再将其除以100转换为小数。
- #include
-
- using namespace std;
-
- // 用于四舍五入
- int round_0 (double n)
- {
- // 若为负数,则先化为正数再进行四舍五入
- if (n > 0)
- return n - int(n) >= 0.5 ? int(n)+1 : int(n);
- else
- return -n - int(-n) >= 0.5 ? -(int(-n) + 1) : -int(-n);
- }
-
- int main()
- {
- double a = 1.2345;
- double b = 1.2355;
- double n_a = -1.2345;
- double n_b = -1.2355;
-
- a = round_0(a * 100.0) / 100.0;
- b = round_0(b * 100.0) / 100.0;
- n_a = round_0(n_a * 100.0) / 100.0;
- n_b = round_0(n_b * 100.0) / 100.0;
-
- cout << a << endl; // 1.23
- cout << b << endl; // 1.24
- cout << n_a << endl; //-1.23
- cout << n_b << endl; // -1.24
- return 0;
- }