Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. isnumeric in c#

isnumeric in c#

Scheduled Pinned Locked Moved C#
csharp
16 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Amar Chaudhary

    but these meathods through exceptions which is a lag in performance :):)

    It is Good to be Important but! it is more Important to be Good

    M Offline
    M Offline
    Martin 0
    wrote on last edited by
    #7

    Hello,

    Amar Chaudhary wrote:

    these meathods through exceptions

    No, thats not true. That's the differents between 'Parse' and 'Convert.ToInt32'. If it's possible for you you should use 'TryParse' in an 'if' == true statement. All the best, Martin

    1 Reply Last reply
    0
    • R rah_sin

      u can try private bool IsNumeric(object numberString) { string strVal = numberString.ToString(); char [] ca = strVal.ToCharArray(); for (int i = 0; i < ca.Length;i++) { if (Char.IsNumber(ca[i])) return false; else return true; } }

      rahul

      E Offline
      E Offline
      ejuanpp
      wrote on last edited by
      #8

      Ithink you just check ca[0]

      1 Reply Last reply
      0
      • R rah_sin

        u can try private bool IsNumeric(object numberString) { string strVal = numberString.ToString(); char [] ca = strVal.ToCharArray(); for (int i = 0; i < ca.Length;i++) { if (Char.IsNumber(ca[i])) return false; else return true; } }

        rahul

        R Offline
        R Offline
        RanjithLogics
        wrote on last edited by
        #9

        gr8 Rahul

        Ranjith Stephen

        J 1 Reply Last reply
        0
        • R RanjithLogics

          gr8 Rahul

          Ranjith Stephen

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #10

          ranjithlogics wrote:

          gr8 Rahul

          Yes "gr8" indeed :rolleyes: as in reading this makes me want to gr8 my forehead with a cheese gr8er. Specifically if (Char.IsNumber(ca[i])) return false; within a method called IsNumeric - whose purpose is to return true when a string is numeric.

          --- How to get answers to your questions[^]

          1 Reply Last reply
          0
          • R rah_sin

            u can try private bool IsNumeric(object numberString) { string strVal = numberString.ToString(); char [] ca = strVal.ToCharArray(); for (int i = 0; i < ca.Length;i++) { if (Char.IsNumber(ca[i])) return false; else return true; } }

            rahul

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #11

            rah_sin wrote:

            for (int i = 0; i < ca.Length;i++) { if (Char.IsNumber(ca[i])) return false; else return true; }

            This logic is so badly flawed.. 1) false is returned if a character is numeric - the purpose of the method was supposed to be returning true if a caracter is numeric 2) It always returns on the first character, rather than checking the whole string is made up of numbers... basically the for loop is useless.

            --- How to get answers to your questions[^]

            R 1 Reply Last reply
            0
            • A Amar Chaudhary

              how can we use isnumeric in c# thanks :):)

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #12

              As has been hinted, rather badly, by previous posters the way to write an isnumeric method is to iterate over each character in a string checking whether Char.IsNumber is true or char == '.' for every character in the string. You may want to remove formatting from the number, as often numbers may be separated with a comma (1,000,000) and sometimes numbers are a representation of currency (£100) - you may or may not want these two cases to be treated as valid numerics.

              --- How to get answers to your questions[^]

              D 1 Reply Last reply
              0
              • J J4amieC

                As has been hinted, rather badly, by previous posters the way to write an isnumeric method is to iterate over each character in a string checking whether Char.IsNumber is true or char == '.' for every character in the string. You may want to remove formatting from the number, as often numbers may be separated with a comma (1,000,000) and sometimes numbers are a representation of currency (£100) - you may or may not want these two cases to be treated as valid numerics.

                --- How to get answers to your questions[^]

                D Offline
                D Offline
                Dan Neely
                wrote on last edited by
                #13

                J4amieC wrote:

                As has been hinted, rather badly, by previous posters the way to write an isnumeric method is to iterate over each character in a string checking whether Char.IsNumber is true or char == '.' for every character in the string.

                You also need to check for '-' in the first char, and that '.' only appears once.

                1 Reply Last reply
                0
                • A Amar Chaudhary

                  but these meathods through exceptions which is a lag in performance :):)

                  It is Good to be Important but! it is more Important to be Good

                  E Offline
                  E Offline
                  Ennis Ray Lynch Jr
                  wrote on last edited by
                  #14

                  I really doubt MS would implement it as such but I would really like to see documentation on the way Int32.TryParse was implemented just so I can see for myself. I suppose, if I wasn't so lazy I could just look at the MSIL ... actually I think I am about to do that.


                  On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage

                  E 1 Reply Last reply
                  0
                  • E Ennis Ray Lynch Jr

                    I really doubt MS would implement it as such but I would really like to see documentation on the way Int32.TryParse was implemented just so I can see for myself. I suppose, if I wasn't so lazy I could just look at the MSIL ... actually I think I am about to do that.


                    On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage

                    E Offline
                    E Offline
                    Ennis Ray Lynch Jr
                    wrote on last edited by
                    #15

                    No try catch. I would post a snippet but it is way long. Int32.TryParse traces all the way up to Number.ParseNumber.


                    On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage

                    1 Reply Last reply
                    0
                    • J J4amieC

                      rah_sin wrote:

                      for (int i = 0; i < ca.Length;i++) { if (Char.IsNumber(ca[i])) return false; else return true; }

                      This logic is so badly flawed.. 1) false is returned if a character is numeric - the purpose of the method was supposed to be returning true if a caracter is numeric 2) It always returns on the first character, rather than checking the whole string is made up of numbers... basically the for loop is useless.

                      --- How to get answers to your questions[^]

                      R Offline
                      R Offline
                      rah_sin
                      wrote on last edited by
                      #16

                      my aim was not to write codes for u rather to suggest the method Char.IsNumber(); now its upto u that how can u use it for ur purpose i think u people are smart enough to write ur ownn logics.

                      rahul

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups