EvilZone

Programming and Scripting => .NET Framework => Topic started by: bubzuru on August 03, 2012, 01:48:11 AM

Title: (C#) IsNumeric
Post by: bubzuru on August 03, 2012, 01:48:11 AM
do i realy need to explain ?
Code: (c#) [Select]
        public bool IsNumeric(string str)
        {
            foreach (Char c in str)
            {
                if (!Char.IsNumber(c))
                {
                    return false;
                }
            }
            return true;
        }
Title: Re: (C#) IsNumeric
Post by: techb on August 03, 2012, 04:15:08 AM
I don't want to sound rude or anything, but why? A quick Google search would yeild the same thing,  or a we quick glance of the doc's.

Either way, I've noticed a lot more activity from you. Nice.
Title: Re: (C#) IsNumeric
Post by: bubzuru on August 03, 2012, 06:52:35 AM
I don't want to sound rude or anything, but why? A quick Google search would yeild the same thing,  or a we quick glance of the doc's.

Either way, I've noticed a lot more activity from you. Nice.

you would think so , but thats the thing there is no inbuilt IsNumeric function in C#
that surprised me to, thats why i posted the function
Title: Re: (C#) IsNumeric
Post by: Kulverstukas on August 03, 2012, 10:19:29 AM
seriously? no built-in function like that? :D lol, Microsoft...
Title: Re: (C#) IsNumeric
Post by: Satan911 on August 03, 2012, 04:57:33 PM
There's no isNumeric function in Java either for the String class. You can use Integer.parseInt which will throw an exception if the string is not a number but that's a really bad use of exceptions. Added a function just like the on Bubzuru posted in my utility class last week.

P.s. There is a isNumeric() function the Apache StringUtils library.
Title: Re: (C#) IsNumeric
Post by: Deque on August 04, 2012, 09:39:10 AM
Your method only works for integers. You should call it isInteger() instead of isNumeric(), otherwise a String like "1.3" will be considered as non-numeric.
Title: Re: (C#) IsNumeric
Post by: bubzuru on August 04, 2012, 11:15:21 PM
Your method only works for integers. You should call it isInteger() instead of isNumeric(), otherwise a String like "1.3" will be considered as non-numeric.

^^ that is true , this function does not work for floating point numbers.
if anybody needs a workaround please post here 
Title: Re: (C#) IsNumeric
Post by: fruitcake2212 on August 08, 2012, 11:07:26 AM
There's no isNumeric function in Java either for the String class. You can use Integer.parseInt which will throw an exception if the string is not a number but that's a really bad use of exceptions.

You can use the TryParse method instead, it will not throw exception.

Also, you could use the IsNumeric function of VisualBasic in your C# application. You would need to reference the visualbasic dll in your project
Title: Re: (C#) IsNumeric
Post by: Deque on August 08, 2012, 12:41:24 PM
TryParse is a C# method, not Java.
Title: Re: (C#) IsNumeric
Post by: fruitcake2212 on August 08, 2012, 12:59:55 PM
TryParse is a C# method, not Java.

Yes sorry I've read Satan911's answer too fast. My answer are possibilities for C#