Is it really this hard to print currency values?

Ken Corey ken at kencorey.com
Sun Jul 29 13:31:20 EDT 2012


It's actually stickier than that, depending on how far and wide your 
currency must be displayed...

Currency can be 1-3 characters ($ or GBP, if you see what I mean)...and 
many of the single letter currency symbols require unicode...take the 
euro "€" for example.  On windows it's alt-0128 (on the keypad).

The thousands separator is '.' in the US, but it's ',' in many parts of 
the world (Europe).

The decimal separator seems to always be the opposite of the thousands 
separator (if the TS is ',', then the decimal is '.').

And how is it formatted if the number goes negative?  (1234.00)?  -1234.56?

I'd probably try something regexpy here...let's see...

Anyway, if you're talking just the US, how about:

put 12345.67 into q
put "" into retval

-- watch out for the wrap here in email...
-- this regexp is meant to be together on one long line.
if 
matchText(q,"([0-9]{0,3}?)([0-9]{0,3}?)([0-9]{0,3}?)([0-9]{0,3}?)\.([0-9]{0,2})$",db,dm,dt,du,dc) 
then
   put db&comma&dm&comma&dt&comma&du&"."&dc into tmp
   put matchText(tmp,",*(.*)",retval) into junk
   put "$"&retval into retval
end if

I'm sure there's a smarter way to do it, but that's a quick approach.

-Ken




More information about the use-livecode mailing list