如何在C++中将字符串转换为double?

34 c++

如何在C++中将字符串转换为double?我想要一个函数,当字符串不是数字时返回0.

Ale*_*son 38

请参阅C++ FAQ Lite 如何将std :: string转换为数字?

请参阅C++ Super-FAQ 如何将std :: string转换为数字?

请注意,根据您的要求,您无法将所有允许的零字符串表示与非数字字符串区分开来.

 // the requested function
 #include <sstream>
 double string_to_double( const std::string& s )
 {
   std::istringstream i(s);
   double x;
   if (!(i >> x))
     return 0;
   return x;
 } 

 // some tests
 #include <cassert>
 int main( int, char** )
 {
    // simple case:
    assert( 0.5 == string_to_double( "0.5"    ) );

    // blank space:
    assert( 0.5 == string_to_double( "0.5 "   ) );
    assert( 0.5 == string_to_double( " 0.5"   ) );

    // trailing non digit characters:
    assert( 0.5 == string_to_double( "0.5a"   ) );

    // note that with your requirements you can't distinguish
    // all the the allowed string representation of zero from
    // the non numerical strings:
    assert( 0 == string_to_double( "0"       ) );
    assert( 0 == string_to_double( "0."      ) );
    assert( 0 == string_to_double( "0.0"     ) );
    assert( 0 == string_to_double( "0.00"    ) );
    assert( 0 == string_to_double( "0.0e0"   ) );
    assert( 0 == string_to_double( "0.0e-0"  ) );
    assert( 0 == string_to_double( "0.0e+0"  ) );
    assert( 0 == string_to_double( "+0"      ) );
    assert( 0 == string_to_double( "+0."     ) );
    assert( 0 == string_to_double( "+0.0"    ) );
    assert( 0 == string_to_double( "+0.00"   ) );
    assert( 0 == string_to_double( "+0.0e0"  ) );
    assert( 0 == string_to_double( "+0.0e-0" ) );
    assert( 0 == string_to_double( "+0.0e+0" ) );
    assert( 0 == string_to_double( "-0"      ) );
    assert( 0 == string_to_double( "-0."     ) );
    assert( 0 == string_to_double( "-0.0"    ) );
    assert( 0 == string_to_double( "-0.00"   ) );
    assert( 0 == string_to_double( "-0.0e0"  ) );
    assert( 0 == string_to_double( "-0.0e-0" ) );
    assert( 0 == string_to_double( "-0.0e+0" ) );
    assert( 0 == string_to_double( "foobar"  ) );
    return 0;
 }
Run Code Online (Sandbox Code Playgroud)


Evg*_*zin 35

最简单的方法是使用boost :: lexical_cast:

double value;
try
{
    value = boost::lexical_cast<double>(my_string);
}
catch (boost::bad_lexical_cast const&)
{
    value = 0;
}
Run Code Online (Sandbox Code Playgroud)

  • @adam,在这种情况下,当my_string中存在非数字数据时,boost :: lexical_cast()抛出.只要my_string通常包含一个数字,那么我会说这符合_exceptional_条件. (4认同)
  • 不应将异常处理用于流量控制.例外情况适用于_exceptional_条件. (2认同)
  • 不幸的是,它看起来像"10.5xxxx".该字符串是否也会失败?还是应该返回10.5? (2认同)

jmu*_*llo 20

atof和strtod做你想要的,但非常原谅.如果你不想接受像"32asd"这样的字符串有效,你需要在这样的函数中包装strtod:

#include <stdlib.h>
double strict_str2double(char* str)
{
    char* endptr;
    double value = strtod(str, &endptr);
    if (*endptr) return 0;
    return value;
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意该代码接受空字符串:)(endptr == str ||*endptr)应修复它 (2认同)

Col*_*lin 7

如果它是一个c-string(以null为终结的char类型数组),你可以这样做:

#include <stdlib.h>
char str[] = "3.14159";
double num = atof(str);
Run Code Online (Sandbox Code Playgroud)

如果它是C++字符串,只需使用c_str()方法:

double num = atof( cppstr.c_str() );
Run Code Online (Sandbox Code Playgroud)

atof()会将字符串转换为double,失败时返回0.该功能记录在此处:http://www.cplusplus.com/reference/clibrary/cstdlib/atof.html


Bad*_*Bad 5

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

int main()
{
    cout << stod("  99.999  ") << endl;
}
Run Code Online (Sandbox Code Playgroud)

输出:(99.999为双精度,空格被自动剥离)

由于C ++ 11转换字符串浮点值(例如双)是可用的功能:
STOF -转换STR为float
STOD -转换STR为双
STOLD -转换STR为长双

我想要一个在字符串不是数字时返回0的函数。

您可以在stod引发异常时添加try catch语句。


Kyl*_*nin 0

我认为atof正是您想要的。该函数解析字符串并将其转换为双精度型。如果字符串不以数字(非数字)开头,则返回 0.0。

但是,它确实尝试解析尽可能多的字符串。换句话说,字符串“3abc”将被解释为 3.0。如果您想要一个在这些情况下返回 0.0 的函数,您需要自己编写一个小包装器。

此外,此函数还适用于以 null 结尾的字符数组的 C 样式字符串。如果您使用的是字符串对象,则在使用此函数之前需要将其转换为 char*。