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!
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
Zax
- Posts: 519
- Joined: Mon May 28, 2007 10:12 am
-
Contact:
Post
by Zax » Wed Mar 03, 2021 4:42 pm
Hello,
I'm trying to get the number of decimals of a passed number, so I wrote:
Code: Select all
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?
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10321
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Wed Mar 03, 2021 4:58 pm
Hi.
Did not look at your code. It was too long. Try this in a button script, having a field with a floating point number in it:
Code: Select all
on mouseUp
answer NumDec(fld 1)
end mouseUp
function numDec var
set the itemDel to "."
return the length of item 2 of var
end numDec
Craig
-
Zax
- Posts: 519
- Joined: Mon May 28, 2007 10:12 am
-
Contact:
Post
by Zax » Wed Mar 03, 2021 5:50 pm
Very simple, thank you Craig

-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10321
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Wed Mar 03, 2021 6:38 pm
I didn't mean to be snarky.
But the trick with LC is to use the little gadgets that are lurking all over the language.
Craig
-
andresdt
- Posts: 156
- Joined: Fri Aug 16, 2019 7:51 pm
-
Contact:
Post
by andresdt » Sat Mar 06, 2021 9:24 pm
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.
Code: Select all
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.
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10321
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Sat Mar 06, 2021 11:43 pm
Andre (?)
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.
Craig
-
Zax
- Posts: 519
- Joined: Mon May 28, 2007 10:12 am
-
Contact:
Post
by Zax » Sun Mar 07, 2021 9:05 am
Thank you andresdt for these precisions.
dunbarx wrote: ↑Sat Mar 06, 2021 11:43 pm
Right on. Data entry validation is an art in and of itself.
So true! Depending on used language, it can be a real headache.