Displaying a number with commas 400,000.00

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!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Displaying a number with commas 400,000.00

Post by trags3 » Mon Apr 14, 2014 4:37 pm

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

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

Re: Displaying a number with commas 400,000.00

Post by dunbarx » Mon Apr 14, 2014 5:29 pm

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

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Displaying a number with commas 400,000.00

Post by trags3 » Tue Apr 15, 2014 2:27 am

Craig
Thanks, that is close to what I expected, but I didn't know how to implement.

Tom

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Displaying a number with commas 400,000.00

Post by jacque » Tue Apr 15, 2014 7:14 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply