Limiting a field to 2 decimal places
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Re: Limiting a field to 2 decimal places
Oh.
You mean find a way to make the thing not work. Why din't you say so?
I did not account for being able to insert a decimal in front of more than two digits, only that you could not "build" such a thing.
Back to the drawing board. But can you play with it as well?
Craig
You mean find a way to make the thing not work. Why din't you say so?
I did not account for being able to insert a decimal in front of more than two digits, only that you could not "build" such a thing.
Back to the drawing board. But can you play with it as well?
Craig
Re: Limiting a field to 2 decimal places
So here is what I put in, just one line to brute-force test each annoying instance of where the thing can fail.
Craig
Code: Select all
on keyDown pKey
set the itemDel to "."
put offset(".",me) into decChar
put word 4 of the selectedChunk of me into charNumber
if pKey = "." and the length of me - charNumber > 2 then exit to top --STUCK THIS LINE IN TO CATCH THOSE PESKY INSERTIONS
if charnumber < decChar then put "Left" into whichSide else put "Right" into whichSide
if pKey = "." and "." is in me then exit keyDown
if pKey is in "0123456789." then
switch
case whichSide = "Left" and pKey <> "."
pass keyDown
break
case whichSide = "Right" and "." is not in item 2 of me
if the length of item 2 of me < 2 then pass keyDown
break
end switch
end if
end keyDown
on enterInField
end enterInField
on returnInField
end returnInField
-
- Posts: 746
- Joined: Sun Feb 04, 2007 11:01 pm
Re: Limiting a field to 2 decimal places
I think we have a winner! That did it. I even set the charNumber > 0 to force the insertion point to be at the end of the string and she worked. 

Tom
MacBook Pro OS Mojave 10.14
MacBook Pro OS Mojave 10.14
Re: Limiting a field to 2 decimal places
Great fun. The closeField suggestion could handle integer only and re-entering decimal point by applying restrictive formatting with numberFormat, which also auto-rounds. Would this help? Terry
Code: Select all
on keyDown X --allow only numbers and 1 decimal point, formatted for 2 decimal places
if X is a number or X = "." and "." is not in me then pass keyDown
end keyDown
on closeField
set the numberFormat to "0.00" --2 decimal places
put me * 1 into me --apply format, auto-rounds
end closeField
on returnInField
closeField
end returnInField
on enterInField
closeField
end enterInField
Beginner Lab (LiveCode tutorial) and StarterKit (my public stacks)
https://tlittle72.neocities.org/openxtalk.html
https://tlittle72.neocities.org/openxtalk.html
-
- Posts: 746
- Joined: Sun Feb 04, 2007 11:01 pm
Re: Limiting a field to 2 decimal places
Thanks, Terry.
Another very good option with a little different approach.
Another very good option with a little different approach.
Tom
MacBook Pro OS Mojave 10.14
MacBook Pro OS Mojave 10.14
Re: Limiting a field to 2 decimal places
Hi,
I did something similar some years ago. My experience was that "the common User™" becomes confused when what it types isn't displayed - its mental capabilities are often less than desired, and instead of realizing that it types something wrong it may cry for the IT department to replace its keyboard ...
So I ended in a solution similar to this:
Even if "the common User™" isn't usually the brightest of the human race, learning by repeating surprisingly often works ;-)
Have fun!
PS: You may omit the "play Fanfare". But keep the other sounds, "the common User™" loves such, and it will greatly help with the learning experience! Just be sure to record them at a low volume ...
I did something similar some years ago. My experience was that "the common User™" becomes confused when what it types isn't displayed - its mental capabilities are often less than desired, and instead of realizing that it types something wrong it may cry for the IT department to replace its keyboard ...
So I ended in a solution similar to this:
Code: Select all
function CheckMyInput tGarbage
-- some code that checks tGarbage, and, if it's valid:
return chrToNum(1) & tGarbage
-- if it's not valid, it makes a guess at what it could mean:
return charToNum(2) & format("%xyz23#!", tGarbage)
-- if it is empty or makes no sense at all:
return empty
end function
on closeField
get CheckMyInput(me)
if char 1 of it is chrToNum(1) then -- we're OK
play "Fanfare"
else if char 1 of it is chrToNum(2) then
delete char 1 of it
put it into me
answer information "Your input was rather strange." & CR & \
"We expect a certain format here, it's [explanation]." & CR & CR & \
"I took a guess, check if it is OK!"
play "Boing"
else
put [defaultValue] into me
answer error "Your input made no sense at all." & CR & \
"We expect a certain format here, it's [explanation]." & CR & CR & \
"It should look like the current entry, try again!"
play "Burp"
end if
end closeField
Have fun!
PS: You may omit the "play Fanfare". But keep the other sounds, "the common User™" loves such, and it will greatly help with the learning experience! Just be sure to record them at a low volume ...
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
Re: Limiting a field to 2 decimal places
Yet another method --> put into script of input field.
Code: Select all
-- Limits input to "money"-format. Selects last 'wrong' input
on textChanged
lock screen; lock messages
put the selectedChunk into sc
if me is not a number then
beep; put word 4 of sc into word 2 of sc --> optional
else
set numberformat to "0.00"
put round(me,2) into me --> 'financial' rounding
end if
do "select sc"
unlock screen; unlock messages
end textChanged
shiftLock happens