how to format decimals?
[1/4] from: sags::apollo::lv at: 25-Apr-2005 17:17
Hi, Rebolers! How to format decimals to show the given number of decimal
fraction digits? F.ex.: format 2.3 3 => 2.300 brgds Janeks
[2/4] 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.
[3/4] from: volker:nitsch:gma:il at: 25-Apr-2005 17:11
On 4/25/05, [SunandaDH--aol--com] <[SunandaDH--aol--com]> wrote:
> Janeks:
> > Hi, Rebolers! How to format decimals to show the given number of decimal
<<quoted lines omitted: 10>>
> 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
> ]
<<quoted lines omitted: 5>>
> 1.2300
> 1.23000
And while looking at it, another way (only short tested):
; digs must be >= 1 !
format: func [num digs] [
int: to-integer num
frac: num - int
frac: form to-integer 10 ** digs * frac ;rebol-order ;)
insert/dup tail frac "0" digs - length? frac
rejoin ["" 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.
>
--
-Volker
Any problem in computer science can be solved with another layer of
indirection. But that usually will create another problem.
David
Wheeler
[4/4] from: greggirwin::mindspring::com at: 25-Apr-2005 9:46
Hi Janeks,
sal> Hi, Rebolers! How to format decimals to show the given number of
sal> decimal fraction digits? F.ex.: format 2.3 3 => 2.300
There is nothing like that built in, unfortunately. Eric Long did a
formatter, which doesn't seem to be in the library. My old notes say
that it didn't work as expected when I tried it--OK for single values,
but not for blocks--maybe due to newer REBOL releases doing something
different.
Eric's design is modeled on sprintf, which isn't really my cup of tea.
I have a start on one myself, but it doesn't support custom formats at
this time (didn't need them myself when I built it). I can send
either, or both, to you if you want. I don't want to post Eric's to
REBOL.org if it's broken though.
In the meantime, here's a quick hack for your specific need:
; NOT LOCALIZED!
pad-decimal: func [value [decimal!] len [integer!] /local s] [
s: form value
head insert/dup tail s #"0" len - ((length? s) - index? find s #".")
]
-- Gregg
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted