Thread Tools Display Modes
11-14-13, 07:28 AM   #1
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
IsNumeric

I need a function which iterates in a string and determines if it's only containts numeric characters, or not.

Nevermind i had a better idea:

Lua Code:
  1. function isNumeric(value)
  2. if value == tostring(tonumber(value)) then
  3.     return true
  4. else
  5.     return false
  6. end
  7. end

Last edited by Resike : 11-14-13 at 07:33 AM.
  Reply With Quote
11-14-13, 08:33 AM   #2
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
tonumber(val)
Lua Code:
  1. if tonumber(val) then print("OK") end
strmatch(val,pattern)
Lua Code:
  1. if strmatch(val,"%d") then print("OK") end

--list
%a --- represents all letters.
%c --- represents all control characters.
%d --- represents all digits.
%l --- represents all lowercase letters.
%p --- represents all punctuation characters.
%s --- represents all space characters.
%u --- represents all uppercase letters.
%w --- represents all alphanumeric characters.
%x --- represents all hexadecimal digits.
%z --- represents the character with representation 0.
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)

Last edited by zork : 11-14-13 at 08:39 AM.
  Reply With Quote
11-14-13, 08:36 AM   #3
Billtopia
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Apr 2009
Posts: 110
I was thinking that this may be better for you but not 100% the pattern is right. It should find any decimal number including decimals with a decimal portion and if that is the only thing on the line (not counting white spaces) then return true

lua Code:
  1. function IsNumeric( data )
  2.     if type(data) == "number" then
  3.         return true
  4.     elseif type(data) ~= "string" then
  5.         return false
  6.     end
  7.     data = strtrim(data)
  8.     local x, y = string.find(data, "[%d+][%.?][%d*]")
  9.     if x and x == 1 and y == strlen(data) then
  10.         return true
  11.     end
  12.     return false
  13. end
  Reply With Quote
11-14-13, 08:51 AM   #4
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by zork View Post
tonumber(val)
Lua Code:
  1. if tonumber(val) then print("OK") end
strmatch(val,pattern)
Lua Code:
  1. if strmatch(val,"%d") then print("OK") end
I don't think the first one would work, because it's converting letters "1234a" -> 1234.

The second one could work with decimals, but would fail on floats and "+", "-" values.

Last edited by Resike : 11-14-13 at 08:53 AM.
  Reply With Quote
11-14-13, 08:56 AM   #5
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by Billtopia View Post
I was thinking that this may be better for you but not 100% the pattern is right. It should find any decimal number including decimals with a decimal portion and if that is the only thing on the line (not counting white spaces) then return true

lua Code:
  1. function IsNumeric( data )
  2.     if type(data) == "number" then
  3.         return true
  4.     elseif type(data) ~= "string" then
  5.         return false
  6.     end
  7.     data = strtrim(data)
  8.     local x, y = string.find(data, "[%d+][%.?][%d*]")
  9.     if x and x == 1 and y == strlen(data) then
  10.         return true
  11.     end
  12.     return false
  13. end
Hmm thats seems good, but i think it's slower then my method.
  Reply With Quote
11-14-13, 10:24 AM   #6
Billtopia
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Apr 2009
Posts: 110
well you did say that you wanted to know if they passed only numbers... converting to a number from the string will convert what (if anything) it can, not test for non numbers included as well
  Reply With Quote
11-14-13, 01:11 PM   #7
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by Billtopia View Post
well you did say that you wanted to know if they passed only numbers... converting to a number from the string will convert what (if anything) it can, not test for non numbers included as well
Yes but i compare that value back to the original one:

value == tostring(tonumber(value))

"1234" string -> converts to 1234 number converts back to "1234" string. (Equal.)

"1234a" string -> converts to 1234 number converts back to "1234" string. (Not equal!)

"-12.34" string -> converts to -12.34 number converts back to "-12.24" string. (Equal.)

"-12.34a" string -> converts to -12.34 number converts badk to "-12.34" string (Not equal.)

Last edited by Resike : 11-14-13 at 01:16 PM.
  Reply With Quote
11-15-13, 05:20 AM   #8
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
How about:

Code:
if strmatch(str, "^%-?[%d%.]+%d$") then print("OK") end
Will match "-2.5" and "-2" and"-.5" and ".5" and "2.5" but not "25." or "2-5" or "25-"
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
11-15-13, 06:41 AM   #9
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Edit: I'm not a wise man.

Last edited by Haleth : 11-15-13 at 09:08 AM.
  Reply With Quote
11-15-13, 06:50 AM   #10
Clamsoda
A Frostmaul Preserver
Join Date: Nov 2011
Posts: 269
tostring() and tonumber() are largely useless in Lua, correct? Lua automatically interprets numbers as numbers or strings depending on the operation.

(Would love input though.)
  Reply With Quote
11-15-13, 08:54 AM   #11
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Originally Posted by Clamsoda View Post
tostring() and tonumber() are largely useless in Lua, correct? Lua automatically interprets numbers as numbers or strings depending on the operation.

(Would love input though.)
tonumber() is used for validation (such as in this situation). Not too sure on tostring().

Also, that reminds me:

I don't think the first one would work, because it's converting letters "1234a" -> 1234.
tonumber() doesn't actually do that. It would just return nil, it doesn't drop characters from strings.

Last edited by Haleth : 11-15-13 at 09:08 AM.
  Reply With Quote
11-15-13, 09:00 AM   #12
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
Originally Posted by Haleth View Post
I don't think you need to use tostring in your updated OP. Lua will automatically convert number to string in comparisons.
This is false. The comparison 5 == "5" will evaluate as false as it should. The comparison 5 < "8" will throw an error.

Originally Posted by Clamsoda View Post
tostring() and tonumber() are largely useless in Lua, correct? Lua automatically interprets numbers as numbers or strings depending on the operation.
Lua will automatically convert a number to a string, or string to a number, in some cases (e.g. addition, concatenation) but not all (e.g. comparisons).

Last edited by Vrul : 11-15-13 at 09:02 AM. Reason: Clarification
  Reply With Quote
11-15-13, 09:09 AM   #13
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Huh. I must've been confusing it with another language - now I need to go back and double check some code I wrote recently my bad.
  Reply With Quote
11-15-13, 10:49 AM   #14
Clamsoda
A Frostmaul Preserver
Join Date: Nov 2011
Posts: 269
Oh I see. I never considered the comparison situation, but I knew for the others. Always piqued my interest when I saw code making a useless tonumber() call.

Thank you for the clarification!
  Reply With Quote
11-15-13, 11:57 AM   #15
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by Haleth View Post
tonumber() doesn't actually do that. It would just return nil, it doesn't drop characters from strings.
You might be right, some other language do that tho.
  Reply With Quote
11-15-13, 02:08 PM   #16
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by Resike View Post
You might be right, some other language do that tho.
He's right.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
11-15-13, 07:15 PM   #17
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Aside from the techinical implementations, you should probably take a hard look at your code and figure out why you're needing to double-check the type of a value like this in the first place.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » IsNumeric


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off