如何将 std::string 转换为 double

问题描述 投票:0回答:7

通常,当我用 C++ 编写任何内容并且需要将

char
转换为
int
时,我只需创建一个等于字符的新
int
即可。

我使用了代码(片段)

 string word;  
 openfile >> word;
 double lol=word;

我收到错误消息

Code1.cpp cannot convert `std::string' to `double' in initialization 

该错误到底意味着什么?第一个词是数字 50。谢谢:)

c++ string file double
7个回答
83
投票
#include <iostream>
#include <string>
using namespace std;

int main()
{
    cout << stod("  99.999  ") << endl;
}

输出:

99.999
(双精度,空格被自动去除)

自 C++11 起,可以使用函数将字符串转换为浮点值(如 double):
stof - 将 str 转换为 float
stod - 将 str 转换为 double
stold - 将 str 转换为 long double

问题中也提到了string到int的转换,C++11中有以下函数:
stoi - 将 str 转换为 int
stol - 将 str 转换为 long
stoul - 将 str 转换为无符号 long
stoll - 将 str 转换为 long long
stoull - 将 str 转换为无符号 long long


82
投票

您可以轻松地将 char 转换为 int,反之亦然,因为对于机器来说,int 和 char 是相同的,都是 8 位,唯一的区别在于它们必须在屏幕上显示,如果数字是 65 并保存为 char ,那么它会显示'A',如果保存为int则会显示65。

对于其他类型,事情会发生变化,因为它们在内存中的存储方式不同。 C 中有一个标准函数可以让你轻松地将字符串转换为双精度数,它就是 atof。 (你需要包含stdlib.h)

#include <stdlib.h>

int main()
{
    string word;  
    openfile >> word;
    double lol = atof(word.c_str()); /*c_str is needed to convert string to const char*
                                     previously (the function requires it)*/
    return 0;
}

17
投票

问题在于 C++ 是一种静态类型语言,这意味着如果某个东西被声明为

string
,那么它就是一个字符串,如果某个东西被声明为
double
,那么它就是一个 double。与 JavaScript 或 PHP 等其他语言不同,无法自动从字符串转换为数值,因为转换可能没有明确定义。例如,如果您尝试将字符串
"Hi there!"
转换为
double
,则没有任何有意义的转换。当然,您可以只需将
double
设置为 0.0 或 NaN,但这几乎肯定会掩盖代码中存在问题的事实。

要解决此问题,请不要将文件内容缓冲到字符串中。相反,只需直接阅读

double

double lol;
openfile >> lol;

这会直接将值读取为实数,如果发生错误将导致流的

.fail()
方法返回 true。例如:

double lol;
openfile >> lol;

if (openfile.fail()) {
    cout << "Couldn't read a double from the file." << endl;
}

5
投票

如果您正在读取文件,那么您应该听取给出的建议并将其放入双精度文件中。

另一方面,如果你确实有一个字符串,你可以使用 boost 的 lexical_cast

这是一个(非常简单)的例子:

int Foo(std::string anInt)
{
   return lexical_cast<int>(anInt);
}

1
投票

下面的程序说明了 C++ 解决转换的方法(不是经典的 C)。请注意,目的是能够使用 iostream 提供的相同格式化工具,例如精度、填充字符、填充、十六进制和操纵器等。

编译并运行这个程序,然后研究它。很简单

#include "iostream"
#include "iomanip"
#include "sstream"
using namespace std;

int main()
{
    // Converting the content of a char array or a string to a double variable
    double d;
    string S;
    S = "4.5";
    istringstream(S) >> d;    
    cout << "\nThe value of the double variable d is " << d << endl;
    istringstream("9.87654") >> d;  
    cout << "\nNow the value of the double variable d is " << d << endl;



    // Converting a double to string with formatting restrictions
    double D=3.771234567;

    ostringstream Q;
    Q.fill('#');
    Q << "<<<" << setprecision(6) << setw(20) << D << ">>>";
    S = Q.str(); // formatted converted double is now in string 

    cout << "\nThe value of the string variable S is " << S << endl;
    return 0;
}

教授。马丁内斯


0
投票

从字符串到双精度的转换可以通过以下方式实现 使用“stdlib.h”库中的“strtod()”函数

#include <iostream>
#include <stdlib.h>
int main () 
{
    std::string data="20.9";
    double value = strtod(data.c_str(), NULL);
    std::cout<<value<<'\n';
    return 0;
}

-4
投票
#include <string>
#include <cmath>
double _string_to_double(std::string s,unsigned short radix){
double n = 0;
for (unsigned short x = s.size(), y = 0;x>0;)
if(!(s[--x] ^ '.')) // if is equal
n/=pow(10,s.size()-1-x), y+= s.size()-x;
else
    n+=( (s[x]-48) * pow(10,s.size()-1-x - y) );
return n;
}

//In case you want to convert from different bases.
#include <string>
#include <iostream>
#include <cmath>
double _string_to_double(std::string s,unsigned short radix){
double n = 0;
for (unsigned short x = s.size(), y = 0;x>0;)
if(!(s[--x] ^ '.'))
n/=pow(radix,s.size()-1-x), y+= s.size()-x;
else
    n+=( (s[x]- (s[x]<='9' ? '0':'0'+7)   ) * pow(radix,s.size()-1-x - y) );
return n;
}
int main(){
std::cout<<_string_to_double("10.A",16)<<std::endl;//Prints 16.625
std::cout<<_string_to_double("1001.1",2)<<std::endl;//Prints 9.5
std::cout<<_string_to_double("123.4",10)<<std::endl;//Prints 123.4
return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.