Page 1 of 1

displaying as currency values (styledText?)

Posted: Fri May 25, 2012 3:39 am
by dochawk
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

Re: displaying as currency values (styledText?)

Posted: Fri May 25, 2012 6:06 am
by dunbarx
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

Re: displaying as currency values (styledText?)

Posted: Fri May 25, 2012 3:03 pm
by hawklaw
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

Re: displaying as currency values (styledText?)

Posted: Fri May 25, 2012 3:39 pm
by dochawk
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.

:?:

Re: displaying as currency values (styledText?)

Posted: Fri May 25, 2012 4:02 pm
by dunbarx
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

Re: displaying as currency values (styledText?)

Posted: Fri May 25, 2012 10:09 pm
by jacque
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

Re: displaying as currency values (styledText?)

Posted: Fri May 25, 2012 10:32 pm
by jacque
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