c++ - 这个 string_to_number 函数有什么问题?

标签 c++ string

我有一个 string_to_number 函数,可以将字符串转换为 double 。为什么这在这种情况下不起作用?

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

double string_to_number( string text )
{
    double value;
    istringstream ( text ) >> value;
    return value;
}

int main()
{
    string text = "1234567890987654321";
    double value = string_to_number( text );
    cout << fixed << setprecision( 0 ) << value << endl; // 123456789098765400 ??? What happened to "321" ?!!

    return 0;
}

最佳答案

这个数字太大,无法放入一个 double 中,因此它被截断了。

关于c++ - 这个 string_to_number 函数有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21601489/

相关文章:

c++ - 通过将文本文件读入字符串/vector 来计算加权/未加权 GPA

php - 如何在 PHP 中从字符串数组创建对象

python - 在python中,有没有一种简单的方法可以将带逗号的数字转换为整数,然后再返回带逗号的数字?

c++ - 如何在不同文件中使用对象 vector

c++ - 这是 C/C++ 中的未定义行为吗(第 2 部分)

c++ - 通过 UDP 发送 XML 数据

arrays - 如何从字符串数组中获取文本并将其用作按钮的文本?

javascript - 如何将人类时间字符串转换为毫秒?

c++ - 为什么 *_iterators 在移除 std::iterator 后仍然需要 typedef something void?

c++ - C++ 中 CPU 使用率降低 : declaring as unsigned int or not?