Hello
I share with you this function that works perfectly for me. Determine if the value of a variable is a numerical value according to the ECMA-404 standard.
function isNumericValue pValue
local tFirstChar
put char 1 of pValue into tFirstChar
if tFirstChar is 0 and char 2 of pValue is not "." or tFirstChar is not in "-0123456789"
then return false
# Scientific notifications
if "e" is in pValue then
set the itemdel to "e"
if the number of items of pValue is 2 and char 1 of pValue is not in".e" and char -1 of pValue is not in".e" then
if char 1 of item 2 of pValue is in "+-" then
delete char 1 of item 2 of pValue
end if
put the item 1 of pValue & the item 2 of pValue into pValue
else
return false
end if
end if
split pValue by "."
if the number of elements of pValue > 2 then return false
if pValue[ 2 ] is empty then return pValue[ 1 ] is an integer
return pValue[ 1 ] is an integer and pValue[ 2 ] is an integer
end isNumericValue
function isNumericValue pValue
local tFirstChar
put char 1 of pValue into tFirstChar
if pValue is not an number then return false
put char 1 of pValue into tFirstChar
if tFirstChar is 0 and char 2 of pValue is not "." and the length of pValue is not 1 or tFirstChar is not in "-0123456789"
then return false
# Scientific notifications
if "e" is in pValue then
set the itemdel to "e"
if the number of items of pValue is 2 and char 1 of pValue is not in".e" and char -1 of pValue is not in".e" then
if char 1 of item 2 of pValue is in "+-" then
delete char 1 of item 2 of pValue
end if
put the item 1 of pValue & the item 2 of pValue into pValue
else
return false
end if
end if
split pValue by "."
if the number of elements of pValue > 2 then return false
if pValue[ 2 ] is empty then return pValue[ 1 ] is an integer
return pValue[ 1 ] is an integer and pValue[ 2 ] is an integer
end isNumericValue
I was reviewing the function and I noticed that it could be optimized a lot. Since if I use the LiveCode function (value is a number) I would only have to worry about the numbers that start with 0 and are not 0 or 0. # and the LiveCode constants that are numbers and other strange ones like infinity. So this is how the new version looks it is considerably smaller and faster.
function _isNumericValue pValue
if pValue is not a number then return false
if char 1 of pValue is 0 and char 2 of pValue is not "." and the length of pValue is not 1 or char 1 of pValue is not in "-0123456789"
then return false
return true
end _isNumericValue
function _isNumericValue pValue
if pValue is not a number then return false
if char 1 of pValue is 0 and char 2 of pValue is not "." and the length of pValue is not 1 or char 1 of pValue is not in "-0123456789"
then return false
return true
end _isNumericValue
function _isNumericValue pValue
if pValue is not a number then return false
return char 1 of pValue is 0 \
and char 2 of pValue is not "." \
and the length of pValue is not 1 \
or char 1 of pValue is not in "-0123456789"
end _isNumericValue
Kind regards,
Thierry
! SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Thanks @Thierry.
In its version, for it to work correctly you must put everything that is returned in the second line between parentheses. Then evaluate whether everything in parentheses is false. Since like this , the opposite of what we want to obtain returns us. (For example for 2 it returns false and 002 returns true.)
In this way we can also put the first (if) on this line and it is even smaller. Also, it is a few milliseconds faster.
Thanks again. This is what it would look like:
function _isNumericValue pValue
return pValue is a number and (char 1 of pValue is 0 \
and char 2 of pValue is not "." \
and the length of pValue is not 1 \
or char 1 of pValue is not in "-0123456789") is false
end _isNumericValue
function _isNumericValue pValue
return pValue is a number \
and not ( char 1 of pValue is 0 \
and char 2 of pValue is not "." \
and the length of pValue is not 1 \
or char 1 of pValue is not in "-0123456789")
end _isNumericValue
and I don't think you'll win few milliseconds,
but the code is certainly more beautiful.
So, what's next
Thierry
! SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!