Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!
function getDecimalNumber numericValue
if (numericValue is not a number) then return -1
if ("." is in numericValue) then
put 20 into maxDecimals
put 1 into multiplier
repeat with i = 1 to maxDecimals
multiply multiplier by 10
get numericValue * multiplier
if (it is an integer) then return i
end repeat
return -1 // too much decimals
else
return 0
end if
end getDecimalNumber
It works if I pass 1.06 to this function (it returns 2), but if I pass 1.16, it return 3, like if 116 was NOT an integer
I don't understand why, but maybe there is a better way to do that?
Not long ago I needed something similar. In the process I went through this thread and noticed that the code for @dunbarx is simple. But there are certain things that you should keep in mind. To avoid possible mistakes in the future. The first thing is to make sure that the parameter is a number and that it has decimals. For the first, I usually use the isNumber function plus the condition that the first character has to be a digit, + or -. This is because for the isNumber function the string pi, inf, and a list of constants are numbers. then you have to see if the number has anything after the. We can do this by counting the number of items. Since every number with tenths must have 2 items if the itemdelimiter is the point. So we would have the following code. Not so simple but much more foolproof.
function numDec pValue
set the itemDel to "."
if pValue is a number and the char 1 of pValue is in "+-0123456789"\
and the number of items of pValue is 2 then return the length of item 2 of pValue
return -1
end numDec
Last edited by andresdt on Sun Mar 07, 2021 2:31 pm, edited 1 time in total.
Right on. Data entry validation is an art in and of itself
I did mention that a "floating point number" was prerequisite for the very basic function I wrote.
Anyway, the devil, and much of the fun and satisfaction lies in the details.