0

I need to check if an std::string is a number. By number I mean either a whole number or a number with a decimal point (base 10).

I'm fine with the limits that long long provides so anything outside that I don't care about. I don't have boost, but have copied the lexical_cast.

It seems like casting to double to verify a string does indeed work, but I'm wondering if there are any corner cases I'm not thinking of.

#include <typeinfo>
#include <sstream>

template<typename Out, typename In> static Out lexical_cast(In input)
{
    stringstream ss;
    ss << input;

    Out r;
    if ((ss >> r).fail() || !(ss >> std::ws).eof())
    {
        throw std::bad_cast();
    }

    return r;
}

bool is_numeric(const string in)
{
    bool success = false;

    try
    {
        lexical_cast<double>(in);
        success = true;
    }
    catch(std::bad_cast &exc)
    {
        success = false;
    }

    return success;
}

EDIT

floating point number

I'm not using C++0x/C++11 so I can't use a regex to easily verify a floating point number. And I'm trying NOT to have to parse the string myself as that just means I have to have more unit tests to make sure I'm not missing anything.

With the NaN I know that they have the property float1 != float1 but GCC screws this up.

LeviX
  • 3,096
  • 3
  • 28
  • 41
  • 1
    Casting to `double` might accept certain textual representations of infinity or NaN. – Kerrek SB Jun 21 '13 at 23:51
  • 1
    Why go to all this trouble, instead of iterating the string and checking if it contains *only* numbers and at most one `.` (or other representation of the decimal point)? – Nik Bougalis Jun 21 '13 at 23:52
  • possible duplicate of [How to say != 0-9 in c ++](http://stackoverflow.com/questions/13080003/how-to-say-0-9-in-c) and [how to check if given c++ string or char* contains only digits?](http://stackoverflow.com/q/8888748/179910) – Jerry Coffin Jun 21 '13 at 23:57
  • 1
    @NikBougalis: only numbers and at most one `.` and perhaps a `+` or a `-` at the beginning, and also maybe a `e` or `E` followed by an optional another `+` or `-` and some more numbers, but not `.`... did I forget anything? – n. m. could be an AI Jun 22 '13 at 00:00
  • @n.m.: So you really want to verify that it's a F.P. number? If so you've duplicated [a different question](http://stackoverflow.com/q/2139715/179910). – Jerry Coffin Jun 22 '13 at 00:05
  • @JerryCoffin: well, I don't, the OP presumably does. – n. m. could be an AI Jun 22 '13 at 00:12
  • Oops -- sorry, responded to the wrong comment. Sorry 'bout that. – Jerry Coffin Jun 22 '13 at 00:16
  • @n.m the OP specifically said "By number I mean either a whole number or a number with a decimal point (base 10)." While you are right that there are many ways to visually represent a number, the OP seems to be talking about a very specific representation, which is trivial to parse and verify. – Nik Bougalis Jun 22 '13 at 01:26
  • @NikBougalis: The OP have said something vague. A number with a decimal point may or may not have the exponential part. Perhaps he means "a floating pont number". English is not everyone's first language. What he has *done* is certainly not limited to the decimal point alone. – n. m. could be an AI Jun 22 '13 at 01:37

0 Answers0