kunshao 题解分享 · 2025/3/8
输出浮点数 - 题解
``` #include <bits/stdc++.h> using namespace std; int main() { double num; cin >> num; // 1. 默认格式(%f),保留6位小数 cout << fixed << setprecision(6) << num << endl; // 2. 固定格式(%f),保留5位小数 cout << fixed << setprecision(5) << num << endl; // 3. 科学计数法格式(%e),保留6位小数 cout << scientific << setprecision(6) << num << endl; // 4. 默认格式(%g),保留6位有效数字 cout << defaultfloat << setprecision(6) << num << endl; return 0; } /* 1. defaultfloat会恢复到%g格式, ——这种格式会根据数值的大小自动选择固定格式或科学计数法, ——并且保留指定的有效数字位数。 2. setprecision 在fixed和scientific模式 下控制小数位数, ——在defaultfloat模式下控制有效数字位数。 */ ```
查看全文
0 0 0 3