On 4/25/05, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>=20
> Janeks:
> > Hi, Rebolers! How to format decimals to show the given number of decima=
l
> >  fraction digits? F.ex.: format 2.3 3 =3D> 2.300
>=20
> 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:
>=20
>  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"]

  insert/dup tail res "0" length - n ;)

>   res
>  ]
>=20
> >> for n 0 5 1 [print format 1.23 n]
> 1.
> 1.2
> 1.23
> 1.230
> 1.2300
> 1.23000
>=20

And while looking at it, another way (only short tested):

; digs must be >=3D 1 !
format: func [num digs] [
=09int: to-integer num
=09frac: num - int
=09frac: form to-integer 10 ** digs * frac ;rebol-order ;)
=09insert/dup tail frac "0" digs - length? frac
=09rejoin ["" int "." frac]
]

for n 0 5 1 [print format 1.23 n]

gives
1.0 ;wrong, but keeps func shorter
1.2
1.23
1.229
1.2300
1.23000

> Sunanda.
> --
> To unsubscribe from the list, just send an email to
> lists at rebol.com with unsubscribe as the subject.
>=20
>=20


--=20
-Volker

"Any problem in computer science can be solved with another layer of
indirection. But that usually will create another problem." David
Wheeler
-- 
To unsubscribe from the list, just send an email to 
lists at rebol.com with unsubscribe as the subject.

Reply via email to