keyInField

Jan Schenkel janschenkel at yahoo.com
Thu Sep 26 17:28:00 EDT 2002


Hi Yves et al,

It took me a bit to construct, and it's a combination
of bits and pieces from earlier posts, but here's the
"definitive" solution for numeric fields *chuckle*

--- Field script for a numeric field

local sBeforePaste

on keyDown pWhichKey
  put the selectedChunk into tChunk
  put word 2 of tChunk into tStart
  put word 4 of tChunk into tStop
  if tStart > tStop -- no actual selection 
    put (char 1 to tStop of me) & pWhichKey \
      into tCheck
    if length(me) > theStart then
      put (char tStart to -1 of me) after tCheck
    end if
  else -- typing would replace the selected text
    put (char 1 to tStart - 1 of me) & pWhichKey \
      into tCheck
    if length(me) > theStop then
      put (char tStop + 1 to -1 of me) after tCheck
    end if
  end if
  if Conv4Calc(tCheck) is a number then pass keyDown
  else beep
end keyDown

-- Reformat upon leaving the field

on closeField
  if "formatField" is not in the pendingMessages \
  then send "formatField" to me in 5 milliseconds
end closeField

on formatField
  put the text of me into tNumber
  put Conv4Disp(tNumber) into me
end formatField

-- Handle pasting of text in this field
-- NOTE : doesn't work when Rev UI is ON

on pasteKey
  put the text of me into sBeforePaste
  if "checkAfterPaste" is not in the pendingMessages \
  then send "checkAfterPaste" to me in 5 milliseconds
  pass pasteKey
end pasteKey

on checkAfterPaste
  put the text of me into tAfterPaste
  if Conv4Calc(tAfterPaste) is not a number then
    beep
    put sBeforePaste into me
  end if
end checkAfterPaste

-- Conversion between calculation and display formats

function Conv4Calc pNumber
  -- strip out the thousand separator (if any)
  put the uThousandSeparator of me into t1000Sep
  if t1000Sep is not empty then
    replace t1000Sep with "" in pNumber
  end if
  -- fix the decimal point for MetaCard/RunRev
  put the uDecimalPoint of me into tDecPoint
  if tDecPoint is not empty and tDecPoint <> "." \
    then replace tDecPoint with "." in pNumber
  -- should now be a regular number
  return pNumber
end Conv4Calc

function Conv4Disp pNumber
  -- convert to the chosen floating point format
  put the uFormat of me into tFormat
  if tFormat is empty then put "%16.2f" into tFormat
  put format(tFormat, pNumber) into tNumber
  -- remove leading spaces
  put 1 into tStart
  repeat while char tStart of tNumber = " "
    add 1 to tStart
  end repeat
  put char tStart to -1 of pNumber into tNumber
  -- prepare the display format
  -- thanks to Ken Ray for this RegExp :-)
  local tMinusHold,tMainNumber,tDecimalHold
  get matchText(tNumber, \
    "([-]?)([0-9]*)[\.]?([0-9*)", \
    tMinusHold, tMainNumber, tDecimalHold)
  -- tweak tDecimalHold
  put the uDecimalPoint of me into tDecPoint
  if tDecPoint is empty then put "." into tDecPoint
  if tDecimalHold is not empty then put tDecPoint \
    before tDecimalHold
  -- now determine how many separators to place
  put the uThousandSeparator of me into t1000Sep
  if t1000Sep is not empty then
    put length(tMainNumber) into tLength
    put (tLength DIV 3) into tSeps
    if (tLength MOD 3) = 0 then subtract 1 from tSeps
    -- insert the commas into the integer part
    repeat with i = tSeps down to 1
      put t1000Sep after char - (i * 3 + 1) of \
        tMainNumber
    end repeat
  end if
  -- put everything back together
  return tMinusHold & tMainNumber & tDecimalHold
end Conv4Calc

--- End of script

Hope this suits everyone's needs ; all you need to do
is fill in the following custom properties
- uDecimalPoint (defaults to ".")
- uThousandSeparator (defaults to empty)
- uFormat (defaults to "%16.2f")

Of course you can always adapt the scripts so that
these custom properties are in the card, stack,...

BeHave fun,

Jan Schenkel.

"As we grow older, we grow both wiser and more foolish
at the same time."  (De Rochefoucald)

--- yves COPPE <yvescoppe at skynet.be> wrote:
> Hi Jan
> 
> 
> in your script for numeric fld, I've understood that
> you can change 
> the numbers after the decimal with
> 
> 
>    put format("%16.3f", tNumber) into tFormatted
> 
> if we change .3f with .2f, it gives 2 numbers after
> decimal.
> 
> Now : 2 problems :
> 
> 1) I'd like the separator of decimal is a comma and
> not a point.
> is it possible ?
> 
> 2) I' d like a separator for the thousands (a space
> for example)
> 
> 
> so : 123456789 ->  1 234 567,89
> 
> can you insert that in your script ?
> 
> thanks.
> 
> -- 
> Greetings.
> 
> Yves COPPE
> 
> Email : yvescoppe at skynet.be

__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com



More information about the use-livecode mailing list