格式化输出

时间:2010-01-29 10:45:12

标签: c++ formatting scientific-notation

我需要以科学计数法输出数字,以致小数点前总是有一个“0”。

e.g。对于数字x = 134.87546,我需要产生输出      0.134875E03 NOT 1.348755E02

有人知道怎么做吗?

先谢谢 - 希拉兹。

3 个答案:

答案 0 :(得分:3)

int exp = (int)log10(input)+1;
double shifted = input / pow(10.0, exp);
printf("%fE%d", shifted, exp);

答案 1 :(得分:0)

int exponent = floor(log10(num)) + 1;

然后只打印“0”,没有小数的数字,“E”,然后是指数。

答案 2 :(得分:0)

您可以使用Boost Format library进行调查,这是一个通用输出格式库。

相关问题