Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ IsFloat function

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
}
like image 962
AndyUK Avatar asked Sep 06 '25 15:09

AndyUK


1 Answers

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(); 
}
like image 103
Bill the Lizard Avatar answered Sep 09 '25 11:09

Bill the Lizard