#include <iostream>
#include <iomanip> // 包含iomanip头文件,用于设置输出格式
int main() {
double num = 123.456789;
// 设置保留小数点后两位
std::cout << std::fixed << std::setprecision(2) << num << std::endl;
// 设置保留小数点后四位
std::cout << std::fixed << std::setprecision(4) << num << std::endl;
return 0;
}
#include <iomanip>
:这个头文件提供了控制输出格式的函数,例如 std::setprecision
。std::fixed
:确保浮点数以固定的小数点格式输出,而不是科学计数法。std::setprecision(n)
:设置浮点数输出时保留的小数位数为 n
位。上一篇:在线编译器c++语言
下一篇:#include
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站