std :: string要float或double [英] std::string to float or double

查看:179
本文介绍了std :: string要float或double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将 std :: string 转换为 float / double
我试过:

  std :: string num =0.6 
double temp =(double)atof(num.c_str());

但它总是返回零。任何其他方式?

解决方案

  std :: string num =0.6 
double temp = :: atof(num.c_str());对我来说,这是一个有效的C ++语法将字符串转换为双精度。 p>

你可以使用stringstream或boost :: lexical_cast来实现,但那些会带来性能损失。






Ahaha你有一个Qt项目...

  QString winOpacity(0.6); 
double temp = winOpacity.toDouble();

额外注释:

如果输入数据为 const char * QByteArray :: toDouble 会更快。


I'm trying to convert std::string to float/double. I tried:

std::string num = "0.6";
double temp = (double)atof(num.c_str());

But it always returns zero. Any other ways?

解决方案

std::string num = "0.6";
double temp = ::atof(num.c_str());

Does it for me, it is a valid C++ syntax to convert a string to a double.

You can do it with the stringstream or boost::lexical_cast but those come with a performance penalty.


Ahaha you have a Qt project ...

QString winOpacity("0.6");
double temp = winOpacity.toDouble();

Extra note:
If the input data is a const char*, QByteArray::toDouble will be faster.

这篇关于std :: string要float或double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆