在c ++中正确显示科学符号

问题描述 投票:-2回答:1
#include <iostream>
#include <iomanip>

using namespace std;
int main()
 {
    long double n;
    cin>>n;
    cout<<setprecision(4)<<fixed<<scientific<<n;

    return 0;
}

例如:如果我在程序中输入256之类的东西,我得到2.5600e + 002作为输出。但是我想打印2.5600 *(10 ^ 2)。我还希望能够控制显示多少位数字小数点后。

有人可以帮我吗?

c++ formatting format scientific-notation
1个回答
0
投票
如评论中所述,该表示法对于科学表示法是正确的。如果要以自己的方式打印它,则必须为此设置一个特殊功能,如下所示:

void PrintScientific(long double d) { int e = 0; if(d < 1 && d > -1) { for(e = 0; d*10 < 10 && d*10 > -10; e--) { d *= 10; } } else { for(e = 0; d/10 > 1 || d/10 < -1; e++) { d /= 10; } } std::cout << std::setprecision(4) << std::fixed << d << "*10^" << e; }

© www.soinside.com 2019 - 2024. All rights reserved.