isnumeric in c#
-
how can we use isnumeric in c# thanks :):)
-
how can we use isnumeric in c# thanks :):)
you could youse double.TryParse or int.TryParse - those methods take two arguments. The first one is the value to parse. The second one is the variable to write to (i guess its always passed as 'out' - look at the method header for further information) The method will return true if the value could be parsed successfully.
-
how can we use isnumeric in c# thanks :):)
You can use this method as there is no built in methods. private bool IsNumeric(object numberString) { string strVal = numberString.ToString(); char [] ca = strVal.ToCharArray(); for (int i = 0; i < ca.Length;i++) { if (ca[i] > 57 || ca[i] < 48) { if(ca[i] != 46 ) return false; } } return true; }
Ranjith Stephen
-
You can use this method as there is no built in methods. private bool IsNumeric(object numberString) { string strVal = numberString.ToString(); char [] ca = strVal.ToCharArray(); for (int i = 0; i < ca.Length;i++) { if (ca[i] > 57 || ca[i] < 48) { if(ca[i] != 46 ) return false; } } return true; }
Ranjith Stephen
nice and should include 43 and 45 also :):)
It is Good to be Important but! it is more Important to be Good
-
you could youse double.TryParse or int.TryParse - those methods take two arguments. The first one is the value to parse. The second one is the variable to write to (i guess its always passed as 'out' - look at the method header for further information) The method will return true if the value could be parsed successfully.
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
-
nice and should include 43 and 45 also :):)
It is Good to be Important but! it is more Important to be Good
-
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
-
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
-
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
gr8 Rahul
Ranjith Stephen
-
gr8 Rahul
Ranjith Stephen
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. -
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
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 can we use isnumeric in c# thanks :):)
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 orchar == '.'
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. -
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 orchar == '.'
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.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.
-
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
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
-
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
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
-
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.