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!
on TestThumPos
put the loc of grc "myCursor" into tLocObjet
put tLocObjet into tLoc2
put the thumbPos of scrollbar "sbSpeed" into tSpeed
add tSpeed to item 2 of tLoc2
put "tSpeed is a number =" && (tSpeed is a number) & cr & "tSpeed=" & tSpeed & cr & "tLocObjet=" & tLocObjet & cr & "tLoc2=" & tLoc2 into tResults
end TestThumPos
tSpeed is a number = true
tSpeed=2
tLocObjet=726,340
tLoc2=726,341.592538
on TestThumPos
put the loc of grc "myCursor" into tLocObjet
put tLocObjet into tLoc2
put the thumbPos of scrollbar "sbSpeed" into tSpeed
put item 2 of loc2 into temp
add tSpeed to temp
-- add tSpeed to item 2 of tLoc2
put "tSpeed is a number =" && (tSpeed is a number) & cr & "tSpeed=" & tSpeed & cr & "tLocObjet=" & tLocObjet & cr & "tLoc2=" & tLoc2 into tResults
answer tResults
end TestThumPos
Just taking item 2 of the loc and putting it into a variable fixes it for me.
If I ran this as you wrote it, with some locs of the grc, you get integers, and with others you do not. There is something in the numberFormat rearing its head here (those six default digits). I initially thought you would have to set the numberFormat to 0. Hey, have you tried this?
on TestThumPos
set the loc of grc "mycursor" to random(600) & "," & random(400)
put the loc of grc "myCursor" into tLocObjet
put tLocObjet into tLoc2
set the numberFormat to "0"
put the thumbPos of scrollbar "sbSpeed" into tSpeed
add tSpeed to item 2 of tLoc2
put "tSpeed is a number =" && (tSpeed is a number) & cr & "tSpeed=" & tSpeed & cr & "tLocObjet=" & tLocObjet & cr & "tLoc2=" & tLoc2 into tResults
answer tResults
end TestThumPos
Changes the grc loc at random and always gives clean integers. Comment the numberFormat line out, and you get six digits after the decimal point.
Of course: truncating takes the integer part of a number. You need to round it:
round (myNumber,0) or the round of myNumber
IMHO, more secure than using the numberFormat:
Dictionnary wrote: Important! Changing the numberFormat does not automatically change the format of a number that's already in a container. It affects numbers only when they are calculated and then displayed or used as strings. Otherwise, the number retains its full numeric precision.
I made a slider with an endvalue of 3. It is possible to get an integral value of the thumbPos if you push the thumb hard right or left. Hard to do in the middle.
if you:
set the numberFormat to "###.###"
answer the thumbposition of scrollbar "test" /3
you get decimal values most places along the slide. So the thumbposition returns an integer, but if you ever have to do any calculations on that value, you must know this. There may be instances where the numberFormat, round function or trunc function is appropriate. Or not; you can create a coarse slider scale, like mine above, to obtain much finer resolution if desired.
In appearance, only integers are returned. Didier is certainly correct when he says that is merely an assumption.
I just have been caught by this one and thought I had a smarter solution:
I noticed the inspector provides a numberFormat field for scrollbars. Having a closer look to the dictionary, I saw that LC allows to set the numberFormat of scrollBars, which is set to empty at creation.
Good.
But unfortunately, "0" doesn't seem to work. If you set it to "0.0", using the inspector or by script using the msg box, it works, the value is stored in the inspector script.
If you set it to 0, by the same ways, "answer the numberformat of scrollbar myScrollBar" returns an empty string.
Bug or feature?
Any idea?
The ability of directly setting the numberFormat would be a smarter solution than getting rid of decimals by script.
Well, well. Yet another gadget that I never saw before.
The "Value Format" in the inspector seems to solve all display worries. If you set it to "0", you get integers. If you set it to "0.00000" you get decimals. No script required.
As for getting the numberformat property, placing a "0" in the inspector will revert to empty when you reload it. I guess "0" and empty are the same.
Try putting in "000". Reload the inspector. One "0" will go away, leaving two. The scrollbar itself will show two leading zeros, as "002, say. So if you place a single "0", it gets deleted as well, reverting back to empty.