displaying as currency values (styledText?)

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
dochawk
Posts: 43
Joined: Wed Sep 02, 2009 9:29 pm

displaying as currency values (styledText?)

Post by dochawk » Fri May 25, 2012 3:39 am

Some of my fields would better be displayed as currency, such as $1,234.56.

Can I use the styled text to leave a number as the field contents, but automatically add a currency representation for what displays, perhaps with styled text? I found http://forums.runrev.com/phpBB2/viewtopic.php?p=12704, which convinced me it's time to stop for the day and read in the morning with a fresh mind, but that thread predates styledText.

My numbers don't change for most fields after being placed, but they are used to calculate others, which livecode can't do from text with commas and dollar signs, I believe.

thans

hawk

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

Re: displaying as currency values (styledText?)

Post by dunbarx » Fri May 25, 2012 6:06 am

Hi.

Er, are you asking if there is a native LC format that permits such things as "$3" or "3,500" to be used in numerical calculations?

No.

But you can swing this in all kinds of ways, behind the scenes, if that is what you meant. Write back.

Craig Newman

hawklaw
Posts: 5
Joined: Sun May 13, 2012 12:08 am

Re: displaying as currency values (styledText?)

Post by hawklaw » Fri May 25, 2012 3:03 pm

I know it isn't going to be native (although 25 years after the release of HyperCard, it's just plain bizarre that fields don't have soda sheet-style formatting!).

I'm still not clear how styledText and the like work (and again find it bizzare that this is lost when copying a field from one card to anothe, requiring a loop through the placed field to copy their styled text).

The main output of my program puts lots of values in boxes--some text, some numerical, some currency. My understanding is that once there are commas and dollar signs, LiveCode can't tret the valu as a number anymore.

My thought s to have the value in the field, but to display its styled text,

so something like instead of using,

Code: Select all

Put amount into field value
I would either do

Code: Select all

send fillYourself to card value
which would do something like

Code: Select all

on fillYourself value
  put value into the target
  If the dataStyle of the target is "currency" then
    put currency(value) into the styledText of the target
   endif
end fillYourself

or perhaps stuffing the value into a custom property, and having it put the currency string into its contents.


And I suppose that currency() would first make a float format of the number, then parse from right to left to add the commas and dollar signs . . .

thanks

hawk

dochawk
Posts: 43
Joined: Wed Sep 02, 2009 9:29 pm

Re: displaying as currency values (styledText?)

Post by dochawk » Fri May 25, 2012 3:39 pm

hmm, I just tried

Code: Select all

set the styledText of field goo to "$" & field goo & ".00"
and now the field displays nothing at all.

:?:

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

Re: displaying as currency values (styledText?)

Post by dunbarx » Fri May 25, 2012 4:02 pm

Setting the styledText creates an array. You would not be able to see its contents unless you use the "combine" command first. Though I get why you started off that way, to use the "styledText" begs the issue of formatted strings being suitable for LC to perform computations.

I have dealt with this sort of thing before, and have always either used one of the methods you already know. Either process the data on the fly with a stripping function

Code: Select all

function goodNumber tText
   repeat for each char theChar in tText
      if theChar is in ".0123456789" then put theChar after temp
   end repeat
   return temp
end goodNumber
or use the stripped value in a custom property of the field.

As Sparkout suggested in a reference in your original post, if you use a custom property, you can use a "setProp" handler to streamline the process. But my preference is to run the visible text through the stripper function, then do the math. When and if you reload the field, you can reformat with "$", etc.


Craig Newman

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

Re: displaying as currency values (styledText?)

Post by jacque » Fri May 25, 2012 10:09 pm

hawklaw wrote:I'm still not clear how styledText and the like work (and again find it bizzare that this is lost when copying a field from one card to anothe, requiring a loop through the placed field to copy their styled text).
That's what the htmlText is good for. It's a quick way to preserve and move styled text. The new array system has its own substantial merits, but for a quickly popping styled text into another field, htmlText is efficient. This is all you need to do:

set the htmltext of fld x to the htmltext of fld y

Code: Select all

Put amount into field value
I think this is just an example, but just in case, don't use "value" as a field or parameter name. "Value" is a keyword and the engine is apt to get confused.

What I usually do is grab the field content, strip it of any currency signs and punctuation, and then do the math. In most cases the first character will be the currency sign, so checking if the first char is a number lets you know if you need to remove it. Then either loop through the remainder checking if each "is a number" or "is an integer" and build a string of only those that qualify, or you can use "replace" to replace common punctuation with empty.

When it's time to put it back, re-format it using something like this:

Code: Select all

put currencyFormat(tNumber) into fld x

function currencyFormat pNum
  if "." is not in pNum then
    set the numberformat to "#.00"
    put value(pNum*1) into pNum -- force number formatting
  end if
  repeat with x = len(pNum)-4 down to 1
    if x mod 3 = 0 then put comma before char x of pNum
  end repeat
  return "$" & pNum
end currencyFormat
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: displaying as currency values (styledText?)

Post by jacque » Fri May 25, 2012 10:32 pm

My example handler fails, so I need to go find the original somewhere. I figured this out at one time.

Edited: I think this is it:

Code: Select all

function currencyFormat pNum
  if "." is not in pNum then
    set the numberformat to "#.00"
    put value(pNum*1) into pNum
  end if
  put char -3 to -1 of pNum into tDecimals
  delete char -3 to -1 of pNum
  if len(pNum) > 3 then
    repeat with x = len(pNum) down to 1 step -3
      put char x-2 to x of pNum before tFNum
      delete char x-2 to x of pNum
      if pNum <> "" then put comma before tFNum
    end repeat
  end if
  put pNum before tFNum
  return "$" & tFNum & tDecimals
end currencyFormat
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply