the easy way to check if the string is a number or not?

Discussion in 'C++' started by kathy, Mar 8, 2006.

  1. kathy

    kathy Guest

    I want to know what is the easy way to check if a string is a number or
    not? the number can be int, float, double, scientific,...

    what is the easy way for only interger?
     
    kathy, Mar 8, 2006
    #1
    1. Advertising

  2. kathy wrote:
    > I want to know what is the easy way to check if a string is a number or
    > not? the number can be int, float, double, scientific,...


    Try to read it using a 'std::istringstream'. If this is not what you
    want, check out Boost's regular expression library and provide a
    regular expression describing the formats you want.
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.eai-systems.com> - Efficient Artificial Intelligence
     
    Dietmar Kuehl, Mar 8, 2006
    #2
    1. Advertising

  3. On 8 Mar 2006 13:34:46 -0800, "kathy" <> wrote:

    >I want to know what is the easy way to check if a string is a number or
    >not? the number can be int, float, double, scientific,...


    check out strtod:
    http://www.opengroup.org/onlinepubs/000095399/functions/strtod.html

    >what is the easy way for only interger?


    strtol

    In both cases you need to understand what the 'endptr' does. Hint:
    strip trailing whitespace before calling strtod or strtol. Encapsulate
    the check in a reusable function.

    Best wishes,
    Roland Pibinger
     
    Roland Pibinger, Mar 8, 2006
    #3
  4. kathy

    BobR Guest

    kathy wrote in message
    <>...
    >I want to know what is the easy way to check if a string is a number or
    >not? the number can be int, float, double, scientific,...
    >what is the easy way for only interger?


    If the others did not give you the answer you wanted, maybe this will help
    you.

    // ----------------------------------------------
    // #include <ccytpe> // ISO C++ 14882: std::isdigit()
    std::cout<<"\n_______ isdigit test _______"<<std::endl;
    int Inum(0);
    double Dnum(0);
    std::string TestForNum("Hi 4 76.5num 42.7e-5");
    std::cout<<"Test string = "<<TestForNum<<"."<<std::endl;
    for(size_t i(0); i < TestForNum.size(); ++i){
    std::cout<<"char at "<<i<<" is "<<TestForNum.at(i)<<", a ";
    if( std::isdigit( TestForNum.at(i) ) ){
    std::istringstream strStream( TestForNum.substr(i) );
    strStream >> Inum;
    strStream.clear();
    strStream.str( TestForNum.substr(i) );
    strStream >> Dnum;
    strStream.clear();
    std::cout<<"digit Inum="<<Inum
    <<" Dnum="<<Dnum<<std::endl;
    }
    else{ std::cout<<"alpha"<<std::endl;}
    }
    std::cout<<"_______ isdigit test _______Done"<<std::endl;
    /* -- output --
    _______ isdigit test _______
    Test string = Hi 4 76.5num 42.7e-5.
    char at 0 is H, a alpha
    char at 1 is i, a alpha
    char at 2 is , a alpha
    char at 3 is 4, a digit Inum=4 Dnum=4
    char at 4 is , a alpha
    char at 5 is 7, a digit Inum=76 Dnum=76.5
    char at 6 is 6, a digit Inum=6 Dnum=6.5
    char at 7 is ., a alpha
    char at 8 is 5, a digit Inum=5 Dnum=5
    char at 9 is n, a alpha
    char at 10 is u, a alpha
    char at 11 is m, a alpha
    char at 12 is , a alpha
    char at 13 is 4, a digit Inum=42 Dnum=0.000427
    char at 14 is 2, a digit Inum=2 Dnum=2.7e-005
    char at 15 is ., a alpha
    char at 16 is 7, a digit Inum=7 Dnum=7e-005
    char at 17 is e, a alpha
    char at 18 is -, a alpha
    char at 19 is 5, a digit Inum=5 Dnum=5
    _______ isdigit test _______Done
    */
    // ----------------------------------------------

    Open <ccytpe> in an editor for a list of more useful items.

    --
    Bob R
    POVrookie
     
    BobR, Mar 9, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen Toledo-Brown
    Replies:
    1
    Views:
    497
  2. Replies:
    5
    Views:
    943
    X-Centric
    Jun 30, 2005
  3. George Mochrie

    easy way to check if a pid is running?

    George Mochrie, Aug 25, 2004, in forum: Ruby
    Replies:
    5
    Views:
    157
    George Mochrie
    Aug 26, 2004
  4. Nate Wiger
    Replies:
    0
    Views:
    83
    Nate Wiger
    Oct 20, 2006
  5. Derek Fountain

    Quick and easy way to check a port

    Derek Fountain, Jul 29, 2005, in forum: Perl Misc
    Replies:
    1
    Views:
    101
    Josef Moellers
    Aug 1, 2005
Loading...

Share This Page