C ++ IsFloat函数 [英] C++ IsFloat function

查看:136
本文介绍了C ++ IsFloat函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道确定字符串值是否合格为浮点数的方便方法?

Does anybody know of a convenient means of determining if a string value "qualifies" as a floating-point number?

BOOL IsFloat( string MyString )
{
   ... etc ...

   return ... // TRUE if float; FALSE otherwise
}


推荐答案

t使用Boost库函数,可以像这样编写自己的isFloat函数。

If you can't use a Boost library function, you can write your own isFloat function like this.

#include <string>
#include <sstream>

bool isFloat( string myString ) {
    std::istringstream iss(myString);
    float f;
    iss >> noskipws >> f; // noskipws considers leading whitespace invalid
    // Check the entire string was consumed and if either failbit or badbit is set
    return iss.eof() && !iss.fail(); 
}

这篇关于C ++ IsFloat函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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