【发布时间】:2010-03-19 07:33:46
【问题描述】:
我希望按照以下规则打印出 double :
1) No scietific notation
2) Maximum decimal point is 3
3) No trailing 0.
例如:
0.01 formated to "0.01"
2.123411 formatted to "2.123"
2.11 formatted to "2.11"
2.1 formatted to "2.1"
0 formatted to "0"
通过使用 .precision(3) 和 std::fixed,我只能实现规则 1) 和规则 2),但不能实现规则 3)
0.01 formated to "0.010"
2.123411 formatted to "2.123"
2.11 formatted to "2.110"
2.1 formatted to "2.100"
0 formatted to "0"
代码示例如下:
#include <iostream>
int main() {
std::cout.precision(3);
std::cout << std::fixed << 0.01 << std::endl;
std::cout << std::fixed << 2.123411 << std::endl;
std::cout << std::fixed << 2.11 << std::endl;
std::cout << std::fixed << 2.1 << std::endl;
std::cout << std::fixed << 0 << std::endl;
getchar();
}
有什么想法吗?
【问题讨论】:
-
三个规则的组合?
标签: c++ formatting