c++ - 如何防止从 "number to string"函数生成的字符串使用科学记数法?

标签 c++ string

我有一个 number_to_string 函数,可以将数字转换为字符串。现在我想输出没有科学记数法的结果字符串。这是我的代码:

#include <iostream>
#include <sstream>
using namespace std;

string number_to_string( double value )
{
    string result;
    stringstream convert;

    convert << value;
    result = convert.str();

    return result;
}

int main()
{
    double d = 591284081248903124;
    string s = number_to_string( d );
    cout << s << endl; // why it shows 5.91284e+17 ?!?!

    return 0;
}

最佳答案

添加convert << std::setiosflags (std::ios::fixed);在行 convert << value; 之前.别忘了 #include <iomanip> .

关于c++ - 如何防止从 "number to string"函数生成的字符串使用科学记数法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21588244/

相关文章:

Python - 将宽字符字符串从二进制文件转换为 Python unicode 字符串

python - 不带空格且带特殊字符的字符串中的词频?

c++ - 从 OpenCV 中的 VideoCapture 中读取每第 n 帧

android - 渲染问题 - Android Studio

C++ 程序无法构建。获取架构错误的 undefined symbol

c++ - 如何使用带有最小化主窗口的 CreateProcess 启动控制台应用程序

python - 如何从字符串中提取所有 UPPER? Python

Python 表情符号搜索和替换未按预期工作

c++ - 为什么类型关键字以 "_t"后缀结尾?

android - C++ 与 iOS/android 的 native 实现