应该使用哪个函数将字符串转换为long double?

时间:2022-09-10 22:31:03

Note that in general, double is different from long double.

请注意,通常,double与long double不同。

strtod converts string to double, but which function should be use to converting string to long double?

strtod将字符串转换为double,但是应该使用哪个函数将字符串转换为long double?

4 个解决方案

#1


14  

In C++03, use boost::lexical_cast, or:

在C ++ 03中,使用boost :: lexical_cast,或者:

std::stringstream ss(the_string);
long double ld;
if (ss >> ld) {
    // it worked
}

In C99, use strtold.

在C99中,使用strtold。

In C89, use sscanf with %Lg.

在C89中,使用带有%Lg的sscanf。

In C++11 use stold.

在C ++ 11中使用stold。

There may be subtle differences as to exactly which formats each one accepts, so check the details first...

关于每个人接受哪种格式可能存在细微差别,因此请先查看详细信息......

#2


6  

You've tagged your question as "C++", so I'm going to give you a C++ answer:

你已经将你的问题标记为“C ++”,所以我将给你一个C ++答案:

Why not just use streams?

为什么不直接使用流?

std::stringstream ss(myString);
long double x;
ss >> x;

#3


1  

In c++, I can only recommend boost::lexical_cast (or in general via the IOStreams).

在c ++中,我只能推荐boost :: lexical_cast(或者通常通过IOStreams)。

In c ? no idea.

在c?不知道。

#4


1  

You can use istream to read long double from string. See here http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/

您可以使用istream从字符串中读取long double。见http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/

If you like scanf family of functions, read with %Lf

如果您喜欢scanf系列函数,请阅读%Lf

#1


14  

In C++03, use boost::lexical_cast, or:

在C ++ 03中,使用boost :: lexical_cast,或者:

std::stringstream ss(the_string);
long double ld;
if (ss >> ld) {
    // it worked
}

In C99, use strtold.

在C99中,使用strtold。

In C89, use sscanf with %Lg.

在C89中,使用带有%Lg的sscanf。

In C++11 use stold.

在C ++ 11中使用stold。

There may be subtle differences as to exactly which formats each one accepts, so check the details first...

关于每个人接受哪种格式可能存在细微差别,因此请先查看详细信息......

#2


6  

You've tagged your question as "C++", so I'm going to give you a C++ answer:

你已经将你的问题标记为“C ++”,所以我将给你一个C ++答案:

Why not just use streams?

为什么不直接使用流?

std::stringstream ss(myString);
long double x;
ss >> x;

#3


1  

In c++, I can only recommend boost::lexical_cast (or in general via the IOStreams).

在c ++中,我只能推荐boost :: lexical_cast(或者通常通过IOStreams)。

In c ? no idea.

在c?不知道。

#4


1  

You can use istream to read long double from string. See here http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/

您可以使用istream从字符串中读取long double。见http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/

If you like scanf family of functions, read with %Lf

如果您喜欢scanf系列函数,请阅读%Lf