如何在std :: cout中以科学方式或定点方式输出浮点数?

2 c++ floating-point visual-c++

#include <iostream>

int main()
{
    float test = 12535104400;

    std::cout << test;

    std::cin.get();
    return 0;
}

//on msvc 2010 this ouputs:  1.25351e+010
Run Code Online (Sandbox Code Playgroud)

我希望它只输出"12535104400"或换句话说,输出没有前导零的人类可读格式,但输出数字的完整值.

ice*_*ime 5

您需要包括<iomanip>:

int main()
{
    const double test = 12535104400;

    std::cout << std::fixed << std::setprecision(0) << test;

    std::cin.get();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)


Nim*_*Nim 5

无法准确表示特定数字,例如,请尝试以下操作:

  float v = 12535104400;
  cout.precision(0);
  cout << fixed << v << endl;
Run Code Online (Sandbox Code Playgroud)

你会看到它输出:12535104512