Author Topic: IsNumeric in C#  (Read 8664 times)

0 Members and 1 Guest are viewing this topic.

LE

  • Guest
Re: IsNumeric in C#
« Reply #15 on: April 18, 2007, 10:40:58 PM »
This is what returns:

Quote
0231.3213 is a number!
asad1213.34 is not a number!
.2342314 is a number!
ADSAD is not a number!
-1 is a number!

LE

  • Guest
Re: IsNumeric in C#
« Reply #16 on: April 18, 2007, 10:50:52 PM »
Luis, that's essentially the solution I posted. yes ?

Yes.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: IsNumeric in C#
« Reply #17 on: April 19, 2007, 12:22:40 AM »
Luis,
strNum6 = "1.32e-5"

With the original version

(isNumeric(strNum6)) //-> 1.32e-5 is a number!

with the overloaded version
(isNumeric(strNum6, NumberStyles.Number))  //-> 1.32e-5 is not a number!
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

LE

  • Guest
Re: IsNumeric in C#
« Reply #18 on: April 19, 2007, 10:11:59 AM »
Hi Kerry;

I noticed that too and also added the test for Thousands, we can use something like:

Code: [Select]
                string strNum6 = "1.32e-5";

                if (isNumeric(strNum6, NumberStyles.Number | NumberStyles.AllowExponent))
                    ed.WriteMessage(strNum6 + " is a number!\n");
                else
                    ed.WriteMessage(strNum6 + " is not a number!\n");

                string strNum7 = "1,200.56";

                if (isNumeric(strNum7, NumberStyles.Number | NumberStyles.AllowThousands))
                    ed.WriteMessage(strNum7 + " is a number!\n");
                else
                    ed.WriteMessage(strNum7 + " is not a number!\n");

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Re: IsNumeric in C#
« Reply #19 on: April 19, 2007, 02:26:24 PM »
Code: [Select]
        private bool isInt32(string inputString)
        {
            bool res = false;
            try
            {
                int temp = Convert.ToInt32(inputString);
                res = true;
            }
            catch (Exception e)
            {
                // Do something with e if you want to
            }
            return res;
        }

You can modify to do numeric or ?
It's amazing what you can do when you don't know what you can't do.
CAD Programming Solutions