C++ 喜欢 atoi 但要漂浮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5017001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
like atoi but to float
提问by lital maatuk
Is there a function similar to atoi which converts a string to float instead of to integer?
是否有类似于 atoi 的函数将字符串转换为浮点数而不是整数?
回答by RED SOFT ADAIR
回答by Fred Foo
回答by sbi
Convert a string to any type (that's default-constructible and streamable):
将字符串转换为任何类型(这是默认可构造和可流式传输的):
template< typename T >
T convert_from_string(const std::string& str)
{
std::istringstream iss(str);
T result;
if( !(iss >> result) ) throw "Dude, you need error handling!";
return result;
}
回答by Cubbi
As an alternative to the the already-mentioned std::strtof()
and boost::lexical_cast<float>()
, the new C++ standard introduced
作为已经提到的std::strtof()
和的替代,boost::lexical_cast<float>()
引入了新的 C++ 标准
float stof(const string& str, size_t *idx = 0);
double stod(const string& str, size_t *idx = 0);
long double stold(const string& str, size_t *idx = 0);
for error-checking string to floating-point conversions. Both GCC and MSVC support them (remember to #include <string>
)
用于错误检查字符串到浮点转换。GCC 和 MSVC 都支持它们(请记住#include <string>
)
回答by Glen
strtof
strtof
From the man page
从手册页
The strtod(), strtof(), and strtold() functions convert the initial portion of the string pointed to by nptr to double, float, and long double representation, respectively.
strtod()、strtof() 和 strtold() 函数将 nptr 指向的字符串的初始部分分别转换为 double、float 和 long double 表示形式。
The expected form of the (initial portion of the) string is optional leading white space as recognized by isspace(3), an optional plus (‘‘+'') or minus sign (‘‘-'') and then either (i) a decimal number, or (ii) a hexadecimal number, or (iii) an infinity, or (iv) a NAN (not-a-number).
字符串(的初始部分)的预期形式是 isspace(3) 识别的可选前导空格、可选加号 (''+'') 或减号 (''-''),然后是 (i ) 十进制数,或 (ii) 十六进制数,或 (iii) 无穷大,或 (iv) NAN(非数字)。
/man page>
/手册页>
atof converts a string to a double (not a float as it's name would suggest.)
atof 将字符串转换为双精度值(不是顾名思义的浮点数。)
回答by Ferdinand Beyer
Use atof
from stdlib.h
:
使用atof
自stdlib.h
:
double atof ( const char * str );
回答by Maxim Egorushkin
Prefer strtof()
. atof()
does not detect errors.
喜欢strtof()
。atof()
不检测错误。
回答by DhruvPathak
This would also work ( but C kind of code ):
这也可以(但 C 类代码):
#include <iostream>
using namespace std;
int main()
{
float myFloatNumber = 0;
string inputString = "23.2445";
sscanf(inputString.c_str(), "%f", &myFloatNumber);
cout<< myFloatNumber * 100;
}
See it here: http://codepad.org/qlHe5b2k
在此处查看:http: //codepad.org/qlHe5b2k
回答by aschepler
#include <stdlib.h>
double atof(const char*);
There's also strtod
.
还有strtod
。
回答by Nova
Try boost::spirit: fast, type-safe and very efficient:
试试 boost::spirit:快速、类型安全且非常高效:
std::string::iterator begin = input.begin();
std::string::iterator end = input.end();
using boost::spirit::float_;
float val;
boost::spirit::qi::parse(begin,end,float_,val);