Limiting a field to 2 decimal places

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Limiting a field to 2 decimal places

Post by dunbarx » Sat Apr 02, 2016 3:09 am

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Limiting a field to 2 decimal places

Post by dunbarx » Sat Apr 02, 2016 3:20 am

So here is what I put in, just one line to brute-force test each annoying instance of where the thing can fail.

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
Craig

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm

Re: Limiting a field to 2 decimal places

Post by quailcreek » Sat Apr 02, 2016 3:42 am

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. :D
Tom
MacBook Pro OS Mojave 10.14

TerryL
Posts: 78
Joined: Sat Nov 23, 2013 8:57 pm

Re: Limiting a field to 2 decimal places

Post by TerryL » Mon Apr 04, 2016 8:03 pm

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

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm

Re: Limiting a field to 2 decimal places

Post by quailcreek » Mon Apr 04, 2016 8:31 pm

Thanks, Terry.
Another very good option with a little different approach.
Tom
MacBook Pro OS Mojave 10.14

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Limiting a field to 2 decimal places

Post by AxWald » Tue Apr 05, 2016 1:27 pm

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:

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
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 ...
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!

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Limiting a field to 2 decimal places

Post by [-hh] » Fri Apr 08, 2016 12:13 pm

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

Post Reply