[REBOL] Re: how to format decimals?
From: SunandaDH:aol at: 25-Apr-2005 10:50
Janeks:
> Hi, Rebolers! How to format decimals to show the given number of decimal
> fraction digits? F.ex.: format 2.3 3 => 2.300
Here's a very quick and dirty method, that reuses oneliner-nfrac.r in the
REBOL.org library -- not optimal in terms of runtime performance, but an example
of software reuse:
nfrac: func [d][length? second parse join d ".." "."]
format: func [number [decimal!] length [integer!]
/local res
][
res: form number
n: nfrac number
if n > length [return copy/part res (length? res) - (n - length) ]
loop length - n [append res "0"]
res
]
>> for n 0 5 1 [print format 1.23 n]
1.
1.2
1.23
1.230
1.2300
1.23000
Sunanda.