std::setprecision用于控制浮点数输出精度,需包含<iomanip>头文件;单独使用时控制总有效位数,与std::fixed结合时控制小数点后位数,与std::scientific结合时控制科学计数法中小数点后位数,配合std::showpoint可强制显示小数点和尾随零。

在C++里,
std::setprecision
std::fixed
std::scientific
要使用
std::setprecision
<iomanip>
std::cout
比如,我们有一个浮点数
M_PI
#include <iostream>
#include <iomanip> // 包含这个头文件才能用 setprecision
int main() {
double pi = 3.14159265358979323846;
double small_num = 0.0000123456789;
double large_num = 123456789.12345;
std::cout << "默认输出 pi: " << pi << std::endl;
std::cout << "默认输出 small_num: " << small_num << std::endl;
std::cout << "默认输出 large_num: " << large_num << std::endl;
std::cout << "\n使用 setprecision(5) 输出 pi: " << std::setprecision(5) << pi << std::endl;
std::cout << "使用 setprecision(5) 输出 small_num: " << std::setprecision(5) << small_num << std::endl;
std::cout << "使用 setprecision(5) 输出 large_num: " << std::setprecision(5) << large_num << std::endl;
return 0;
}运行上面这段代码,你会发现
std::setprecision(5)
std::fixed
立即学习“C++免费学习笔记(深入)”;
std::setprecision
std::fixed
这大概是
setprecision
std::setprecision
std::fixed
std::fixed
std::setprecision
举个例子,如果我需要显示一个数值,并且总是精确到小数点后两位,那么
std::fixed
std::setprecision(2)
#include <iostream>
#include <iomanip>
int main() {
double price = 19.9987;
double tax_rate = 0.065;
double total_cost = price * (1 + tax_rate);
double interest = 123.456789;
std::cout << "原始价格: " << price << std::endl;
std::cout << "原始总成本: " << total_cost << std::endl;
std::cout << "原始利息: " << interest << std::endl;
std::cout << "\n使用 fixed 和 setprecision(2) 格式化输出:\n";
std::cout << std::fixed << std::setprecision(2); // 一旦设置,会持续影响后续输出
std::cout << "格式化价格: " << price << std::endl;
std::cout << "格式化总成本: " << total_cost << std::endl;
std::cout << "格式化利息: " << interest << std::endl;
// 注意,这里如果不取消 fixed,后面的输出仍会受影响
std::cout << "\n如果我想显示更多位,但仍然是 fixed 模式:\n";
std::cout << std::setprecision(5) << interest << std::endl; // 此时是小数点后5位
return 0;
}你会发现,一旦
std::fixed
std::defaultfloat
std::scientific
当我们不使用
std::fixed
std::scientific
std::setprecision
这个行为在某些科学计算场景下可能很有用,比如你关心的是测量结果的总体精度,而不是小数点后的具体位数。
#include <iostream>
#include <iomanip>
int main() {
double very_small_positive = 0.000000123456789;
double large_value = 987654321.123456789;
double medium_value = 123.456789;
std::cout << "默认模式下,setprecision(5) 控制总有效位数:\n";
std::cout << std::setprecision(5); // 设置总有效位数为5
std::cout << "极小值: " << very_small_positive << std::endl;
std::cout << "大值: " << large_value << std::endl;
std::cout << "中等值: " << medium_value << std::endl;
std::cout << "\n切换到 setprecision(10) 再看:\n";
std::cout << std::setprecision(10); // 设置总有效位数为10
std::cout << "极小值: " << very_small_positive << std::endl;
std::cout << "大值: " << large_value << std::endl;
std::cout << "中等值: " << medium_value << std::endl;
return 0;
}从输出中你会看到,当数字很大时,
setprecision(5)
std::setprecision
std::showpoint
std::scientific
std::setprecision
std::showpoint
std::scientific
std::showpoint
std::setprecision
setprecision
#include <iostream>
#include <iomanip>
int main() {
double whole_number = 123.0;
double decimal_number = 456.7;
std::cout << "默认输出:\n";
std::cout << whole_number << std::endl;
std::cout << decimal_number << std::endl;
std::cout << "\n结合 showpoint 和 setprecision(5) (默认模式,控制总有效位数):\n";
std::cout << std::showpoint << std::setprecision(5);
std::cout << whole_number << std::endl; // 123.00
std::cout << decimal_number << std::endl; // 456.70
std::cout << "\n结合 showpoint, fixed 和 setprecision(5) (控制小数点后位数):\n";
std::cout << std::fixed << std::setprecision(5); // fixed 模式下,showpoint 行为更明显
std::cout << whole_number << std::endl; // 123.00000
std::cout << decimal_number << std::endl; // 456.70000
// 记得切换回默认模式,否则会影响后续输出
std::cout << std::defaultfloat << std::noshowpoint;
std::cout << "\n切换回默认后: " << whole_number << std::endl;
return 0;
}你会发现,
std::showpoint
123.0
setprecision(5)
123.00
123.00000
std::scientific
std::setprecision
std::scientific
1.2345e+02
std::setprecision
std::fixed
#include <iostream>
#include <iomanip>
#include <cmath> // For M_PI
int main() {
double pi = M_PI; // 3.1415926535...
double very_small = 0.000000000123456789;
double very_large = 123456789012345.0;
std::cout << "默认输出:\n";
std::cout << pi << std::endl;
std::cout << very_small << std::endl;
std::cout << very_large << std::endl;
std::cout << "\n结合 scientific 和 setprecision(3) (控制小数点后3位):\n";
std::cout << std::scientific << std::setprecision(3);
std::cout << pi << std::endl;
std::cout << very_small << std::endl;
std::cout << very_large << std::endl;
std::cout << "\n结合 scientific 和 setprecision(8) (控制小数点后8位):\n";
std::cout << std::setprecision(8); // 注意 scientific 状态仍然保持
std::cout << pi << std::endl;
std::cout << very_small << std::endl;
std::cout << very_large << std::endl;
// 切换回默认浮点输出模式
std::cout << std::defaultfloat;
std::cout << "\n切换回默认后: " << pi << std::endl;
return 0;
}在科学计数法模式下,
setprecision(N)
总的来说,
std::setprecision
以上就是c++++中setprecision怎么用的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号