1

I'm trying to search for the beginning of a number. isdigit is there a function that can include negatives in isdigit? So that it will match the first character of the number: "-13"?

Ron
  • 14,674
  • 4
  • 34
  • 47
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • 4
    `-` isnt really a digit, so whats wrong with `isdigit(c) || (c=='-')`? – 463035818_is_not_an_ai Oct 12 '18 at 13:19
  • 1
    *I'm trying to search for the beginning of a number.* -- Maybe there is no need for you to do the search, and other means of extracting the number are available. What is the scenario where you need to search for the number? – PaulMcKenzie Oct 12 '18 at 13:22
  • 1
    You better use regex for such search, it will find where that numbers starts and where it stops. – Slava Oct 12 '18 at 13:22
  • 2
    Reminder: numbers can have an optional `+` in front of them also. – Thomas Matthews Oct 12 '18 at 14:04
  • Note that `+1.000.000,00` and `65,535` and `6.022x10^23` are all numbers. Will your search find them? – Tim Randall Oct 12 '18 at 15:04
  • @TimRandall I mean if it matched +/-/`isdigit` it would... note the question is about finding *the begining* of a number. – Jonathan Mee Oct 12 '18 at 15:08
  • @JonathanMee all I'm getting at is that you might want a higher-level search/parse function. Bathsheba also mentioned that accountants use `(negative_number)` format. It's complicated – Tim Randall Oct 12 '18 at 15:10
  • @TimRandall I'm not sure what you mean... I just want to identify the first character of numbers. – Jonathan Mee Oct 12 '18 at 15:11

3 Answers3

4

No there isn't. A digit is one thing that comprises a representation of a number, along with the radix, sign, decimal separator, thousands separator, and perhaps other things such as exponentation.

If - was emitted into isdigit then you could expect some folk to want + to be included too, and perhaps even ( and ) to keep the accountants happy.

So in summary, testing if something is a representation of a number is an entirely different beast to testing if a particular character is a digit.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
2

There is not, but it is trivial to make one

bool my_isdigit(unsigned char ch)
{
    return ch == '-' || std::isdigit(ch);
}

If you think about what a digit is this makes sense. According to the standard a digit is defined as [0-9] and as detailed here, there aren't actually any negative literals in C++. It is a positive number that gets the unary operator - applied to it.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • 1
    Personally I'd prefer the type of `ch` to be `int`. – Bathsheba Oct 12 '18 at 13:42
  • @Bathsheba Any particular reason for that? – NathanOliver Oct 12 '18 at 13:42
  • Yup, cuz it's the type of `std::isdigit` innit (then you can pass EOF into your function without a nasty wraparound conversion to unsigned char). – Bathsheba Oct 12 '18 at 13:43
  • @Bathsheba It is but *The behavior is undefined if the value of ch is not representable as unsigned char and is not equal to EOF*. So why not just start with an `unsigned char`, instead of having to explicitly cast it? – NathanOliver Oct 12 '18 at 13:44
  • @NathanOliver https://stackoverflow.com/questions/37482246/which-tolower-in-c#comment62474094_37482246 – Jonathan Mee Oct 12 '18 at 13:46
  • 1
    I mean this doesn't handle EOF in it's current condition... Additionally, aren't even char equality tests generally run by first promoting both sides to an `int`? – Jonathan Mee Oct 12 '18 at 13:49
  • @JonathanMee Per [this](https://stackoverflow.com/questions/21805674/do-i-need-to-cast-to-unsigned-char-before-calling-toupper-tolower-et-al) you have to cast the value you pass to these functions to an `unsigned char` which means taking an `unsigned char` in the first place, or casting the parameter, will give you the same behavior. – NathanOliver Oct 12 '18 at 13:53
  • @NathanOliver And it finally clicks, this is the only way to force the number to be `unsigned`. Thank you. Have a +1. – Jonathan Mee Oct 12 '18 at 13:57
1

No, because std::isdigit is only for checking if a single character is a (decimal) digit or not, not a number.

If you want to check if a string contains a valid number (negative or not), then you can either attempt to parse it yourself (it's actually rather trivial) or use e.g. std::stoi (and family) which besides the conversion (that you can throw away the result of) also include verification of the input string.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621