我有一些集合返回的字段为
2.4200
2.0044
2.0000
Run Code Online (Sandbox Code Playgroud)
我想得到的结果
2.42
2.0044
2
Run Code Online (Sandbox Code Playgroud)
我尝试过String.Format,但它返回2.0000并将其设置为N0舍入其他值.
在C++中输出浮点数的最优雅方法是什么,没有科学记数法或尾随零?
float a = 0.000001f;
float b = 0.1f;
cout << "a: " << a << endl; // 1e-006 terrible, don't want sci notation.
cout << "b: " << b << endl; // 0.1 ok.
cout << fixed << setprecision(6);
cout << "a: " << a << endl; // 0.000001 ok.
cout << "b: " << b << endl; // 0.100000 terrible, don't want trailing zeros.
Run Code Online (Sandbox Code Playgroud)