Page 1 of 1

Displaying a number with commas 400,000.00

Posted: Mon Apr 14, 2014 4:37 pm
by trags3
I have a text entry field that needs to be a number.
Here is the code for the entry field.

on keyDown theKey
if theKey is not a number and theKey is not "." then beep
else pass keyDown
end keyDown

I perform a multiplication on this field on exitField and this works fine only allowing numbers and the decimal point to be entered.
If I allow the comma to be entered in the proper locations ie 400,000.00 then the multiplication gives an error as the comma causes the field to not be evaluated as a number.
How can I allow commas and still do a multiplication?

Tom

Re: Displaying a number with commas 400,000.00

Posted: Mon Apr 14, 2014 5:29 pm
by dunbarx
Hi.

I guess you will have to think of this as two separate "threads", one of which is the number you display, and the other the number you calculate with. Try this function in the card script:

Code: Select all

function goodNumber tText
   repeat for each char tChar in tText
      if tChar is in ".0123456789" then put tChar after temp
   end repeat
   return temp
end goodNumber
You could then say:

put goodNumber( "123,456,789.99") + goodNumber("3,000") into fld "answers"

Craig Newman

Re: Displaying a number with commas 400,000.00

Posted: Tue Apr 15, 2014 2:27 am
by trags3
Craig
Thanks, that is close to what I expected, but I didn't know how to implement.

Tom

Re: Displaying a number with commas 400,000.00

Posted: Tue Apr 15, 2014 7:14 pm
by jacque
Since your field already filters out non-numerical entries, you can shorten the function this way:

Code: Select all

function goodNumber tText
  replace comma with empty in tText
  return tText
end goodNumber
And since that is only one line of code, you probably don't need a function, you can just put it directly into the processing handler before you do the multiplication.