Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Translate this code into real Rebol?

From: sunandadh:aol at: 23-Jan-2002 6:35

Here's a useful function (I think). It "pretty prints" numbers and currency amounts. Examples of output:
>> pretty-print-number 123
== "123"
>> pretty-print-number 12345
== "12,345"
>> pretty-print-number $12345
== "£12,345.00"
>> pretty-print-number -12345
== "-12,345"
>> pretty-print-number/brackets -12345
== "(12,345)"
>> pretty-print-number/brackets -$12345
== "(£12,345.00)"
>> pretty-print-number/brackets -$12345.68
== "(£12,345.68)" You can change the currency symbol, decimal and thousands separators, though not easily (eg for India) the digit groupings. It's typical of the sort of code I knock out in a hurry--in this case for the prototype I mention in an earlier post. It works, but it could almost be Basic. Is there a more Rebolish (Rebellious? Rebvolting?) way of doing this? = = = = = = = = = = = = = = = = = rebol [] Pretty-print-number: func [in-amount [Money! Integer! Decimal!] /Brackets /local work out-string thousands-sep decimal-sep currency-sign ][ thousands-sep: "," decimal-sep: "." currency-sign: "£" work: parse/all to-string in-amount decimal-sep out-string: copy "" if (length? work) > 1 [ insert out-string join decimal-sep work/2] while [(length? work/1) > 3] [ insert out-string copy skip work/1 ((length? work/1) - 3) insert out-string thousands-sep work/1: copy/part work/1 ((length? work/1) - 3) ] insert out-string work/1 ;; Fix the messes (like -,123 and $,123,456.78) ;; ============================================ replace out-string join "-" thousands-sep "-" replace out-string "$," "$" replace out-string "$" currency-sign if all [negative? in-amount Brackets ][ replace out-string "-" "" out-string: join "(" [out-string ")"] ] return out-string ] ; func = = = = = = = = = = = = = = = = Thanks, Sunanda.