# Validate double number

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 01-14-2008
guaro555
Validate double number
Hi gurus

I need to validate an input from the user and to make sure is a valid number, I've seemed multiple threads on integer but I really need to validate a double

123.34 or 345.23 this kind of number and make sure is a valid number

Thanks a bunch gurus
• 01-14-2008
Elysia
Code:

`if (strtod(buf, NULL) == 0 && buf[0] != '0'); /* Invalid number - not a double! */`
• 01-14-2008
robwhit
Code:

`if (strtod(buf) == 0 && buf[0] != '0') /* Invalid number - not a double! */ ;`
fixed. :)
• 01-14-2008
Elysia
Hehehe. Typically I just put an empty if statement since I have nothing to put in the if. Just to demonstrate :)
• 01-14-2008
Dino
• 01-14-2008
Dino
well, bad example. I guess the only one that would fail would be -0.
• 01-14-2008
guaro555
Thanks gurus

Is there a generic validation in case the user goes as follow

123.45
123.00
123
-123.45

Thanks for battling with me here
• 01-14-2008
Dino
Elysia's example will work for all those values. It's a generic validation. Do you understand why / how it works?
• 01-14-2008
Elysia
It will only fail for -0, but who in their right mind types that? A little additional logic can fix that, though. Also forgot that strtod takes two arguments. Updated reply.
• 01-14-2008
robwhit
Quote:

Originally Posted by robwhit
Code:

`if (strtod(buf) == 0 && buf[0] != '0') /* Invalid number - not a double! */ ;`
fixed. :)

Aha! Checked the docs:
Code:

`if (strtod(buf, NULL) == 0 && buf[0] != '0') /* Invalid number - not a double! */ ;`
edit: ARRGHGH! beaten.
• 01-14-2008
Elysia
If you are really picky about detecting -0, this will work:
Code:

`if ( strtod(buf, NULL) == 0 && (buf[0] != '0' && (buf[0] != '\0' && buf[0] != '-' && buf[1] != '0')) ) /* Not a double! */;`
• 01-14-2008
guaro555
Not really T but i case you fill like a brushing your teaching skills with me andexplain I can alway search for it and learn how it works

thanks again gurus, I though C was going to be easy!!!
• 01-14-2008
robwhit
What makes you think it won't accept -0? Or any negative number?
• 01-14-2008
Elysia
What the code does is first convert the inputted string (buf) to a double with a call to strtod. Then it checks if the result is 0. Strtod returns 0 in any of the following situations: the string is not a double, or not a number, or the user entered 0. So then I proceed to check the first element in the inputted string (buf[0]) and check if it's '0', too. If it is, then the user entered 0. If it isn't '0', then the user entered a non-number.

The more complicated example first checks if buf[0] (first input character) is not '0' and also checks if buf[0] is not '-' and the second character, buf[1] is not '0'. That would mean something like, if the first two characters aren't "-0".
So in essence, if the converted double is 0 AND the first character entered is NOT '0' AND the first two entered character aren't "-0" THEN it's not a valid double.

Quote:

Originally Posted by robwhit
What makes you think it won't accept -0? Or any negative number?

The first example won't accept -0 because it's the same as 0, so strtod(buf, NULL) == 0 is true BUT buf[0] is NOT 0, it's actually -, so it assumes it's not a double and fails.
The second example fixes that.
It does accept negative numbers, however.
• 01-14-2008
guaro555
So ely (Buf) is my user value entry? what does the Null do, now that you are on the roll

Thanks a bunch again
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last