std::isdigit(std::locale)

From cppreference.com
< cpplrm; | locale
Defined in header <locale>
template< class charT >
bool isdigit( charT ch, const locale& loc );

Checks if the given character is classified as a digit by the given locale's std::ctype facet.

Parameters

ch - character
loc - locale

Return value

Returns true if the character is classified as a digit, false otherwise.

Possible implementation

template< class charT >
bool isdigit( charT ch, const std::locale& loc ) {
    return std::use_facet<std::ctype<charT>>(loc).is(std::ctype_base::digit, ch);
}

Example

#include <iostream>
#include <locale>
#include <string>
#include <set>

struct jdigit_ctype : std::ctype<wchar_t>
{
    std::set<wchar_t> jdigits{L'',L'',L'',L'',L'',L'',L'',L'',L'',L''};
    bool do_is(mask m, char_type c) const {
        if ((m & digit) && jdigits.count(c))
            return true; // Japanese digits will be classified as digits
        return ctype::do_is(m, c); // leave the rest to the parent class
    }
};

int main()
{

    std::wstring text = L"123";
    std::locale loc(std::locale(""), new jdigit_ctype);

    std::locale::global(std::locale(""));
    std::wcout.imbue(std::locale());

    for(wchar_t c : text)
        if(std::isdigit(c, loc))
            std::wcout << c << " is a digit\n";
        else
            std::wcout << c << " is NOT a digit\n";
}

Output:

1 is a digit
2 is a digit
3 is a digit
 is a digit
 is a digit
 is a digit
 is NOT a digit
 is NOT a digit
 is NOT a digit

See also

checks if a character is a digit
(function)
checks if a wide character is a digit
(function)