What Is the Difference Between Isdigit() and Isnumber()?

I have the function isnumber() in my ctype.h. I don’t find references of this function in the books I have neither in for consult. I intend to detect and show the difference between isdigit() and isnumber() by a simple example, but there is not (besides, U+0C6A, U+0ED2 and ½ are undetectable by the functions). Is there any difference? How could I resolve that?

int main(int, char *[])
{
    string text("Hello½123, Poly౪ ໒girl0.5!!!");
    decltype(text.size()) punct = 0, alpha = 0, number = 0, space = 0, digit = 0, graf = 0;
    for(auto i : text) {
        if(ispunct(i)){
            cout << i << "<punct  ";
            ++punct;
        }
        if(isalpha(i)) {
            cout << i << "<alpha  ";
            ++alpha;
        }
        if(isnumber(i)) {
            cout << i << "<number  ";
            ++number;
        }
        if(isdigit(i)) {
            cout << i << "<digit  ";
            ++digit;
        }
        if(isgraph(i)) {
            cout << i << "<graph  ";
            ++graf;
        }
        if(isspace(i))
            ++space;
    }
    cout << endl << "There is " << endl;
    cout << punct <<  " puncts," << endl;
    cout << alpha << " alphas," << endl;
    cout << number << " numbers," << endl;
    cout << digit << " digits," << endl;
    cout << graf << " graphs," << endl;
    cout << space << " spaces" << endl;
    cout << "in " << text << endl;

    return 0;
}

A part of the result:

...

5 numbers,
5 digits,
23 graphs,
2 spaces
in Hello½123, Poly౪ ໒girl0.5!!!
1

3 Answers

isnumber() may be an Apple-specific C++ method (I don't have a Mac on hand to check). You can see it in the Apple dev guide:

The isnumber() function behaves similarly to isdigit(), but may recognize additional characters, depending on the current locale setting.


Besides, isnumber() is not declared on Linux: I use g++ 6.1.1 on Linux 4.7.2 and get the error:

g++ a.cpp
a.cpp: In function 'int main(int, char**)':
a.cpp:20:17: error: 'isnumber' was not declared in this scope
   if (isnumber(i)) {
                 ^

I also use clang3.8.1 to test:

clang++ a.cpp --std=c++11
a.cpp:20:7: error: use of undeclared identifier 'isnumber'
                if (isnumber(i)) {
                    ^
1

isdigit() only works with 0-9.

isnumber() allows other number values such as fractions. Some characters that are 'numeric' but not digits include 0x00b2 and 0x00b3 which are superscripted 2 and 3 ('²' and '³') and the glyphs that are fractions such as '¼', '½', and '¾'.

4

The isnumber() function was added by Apple, so you need to use Apple documentation.

Here's a quote from the ctype.h manpage for iOS†:

STANDARDS
     These functions, except for digittoint(), isascii(), ishexnumber(),
     isideogram(), isnumber(), isphonogram(), isrune(), isspecial() and
     toascii(), conform to ISO/IEC 9899:1990 (``ISO C90'').

The same page doesn't actually link to a manpage for isnumber(), but I extrapolated its URL from the links to manpages for the standard functions, and found this:

The isnumber() function behaves similarly to isdigit(), but may recognize
additional characters, depending on the current locale setting.

† Result #4 on Google for isnumber ctype…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest