-2

I have simple function checking if string is number or not. Suddenly I discovered that it don't work with "0" or "00". Tell me why, please! And how to make it works?

string num = "00";
Int32.TryParse(num, out int n);
if (n > 0) return true; // It works nice on any digits except 0 and 00.

Also I tried:

double.TryParse(num, out double n);

But don't work too.

So I went in such way:

if ((n > 0) | (num == "0") | (num == "00")) return true;
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Anri
  • 11
  • 2
  • Does this answer your question? [How can I check if a string is a number?](https://stackoverflow.com/questions/6733652/how-can-i-check-if-a-string-is-a-number) – Danilo Mercado Oudalova Oct 03 '22 at 09:29
  • When it say "it don't work", what exactly do you mean? Is n null? Does it throw an error? True will only be returned if it's greater than 0, so even if num is converted it won't return true as it's not greater than 0. – sr28 Oct 03 '22 at 09:33
  • 2
    Please don't add unrelated tags. This question has no Windows Forms specific code. Also you should use `||` (logical or) not `|` (bitwise or), because if you use the bitwise or, you'll end up evaluating `n > 0` AND `num == "0"` AND `num == "00"` even if `n > 0` was already `true`, which is unnecessary work. The logical or (`||`) will short-circuit this. – ProgrammingLlama Oct 03 '22 at 09:33

2 Answers2

7

Of course 0 is not greater than 0, what did you expect? :)

int.TryParse returns a boolean letting you know if parsing the number was successful:

Returns

Boolean

true if s was converted successfully; otherwise, false.

So what you oughta do is the following:

return int.TryParse(num, out int _);
knittl
  • 246,190
  • 53
  • 318
  • 364
  • I made such way: if (Int32.TryParse(num, out int n)) return true; Thanks! – Anri Oct 03 '22 at 12:28
  • 1
    @Anri sure, if there is something else to handle for non-numbers, that's the way to go. But if you have `if (condition) return true; else return false;` then you might as well simplify this to `return condition` – knittl Oct 03 '22 at 12:29
3

You can create a function and call it to check if it is a number or not.

class Program
{
    static void Main(string[] args)
    {
        string num = "00";
        if (IsNumber(num))
            Console.WriteLine($"'{num}' is a number");
        else
            Console.WriteLine($"'{num}' is not a number");
    }

    private static bool IsNumber(string num)
    {
        return int.TryParse(num, out _);
    }
}

Update:
With decimal.TryParse(num, out _) you can check if a string is an integer, double or decimal number.

class Program
{
    private static void Main(string[] args)
    {
        var num = "0.07";
        Console.WriteLine(IsNumber(num) ? 
            $"'{num}' is a number" : 
            $"'{num}' is not a number");
    }
    private static bool IsNumber(string num) => 
        decimal.TryParse(num, out _);
}

Demo: https://dotnetfiddle.net/9LgobJ

Ibrahim Timimi
  • 2,656
  • 5
  • 19
  • 31