[REBOL] Re: Limiting Precision of Decimals,
From: g:santilli:tiscalinet:it at: 25-May-2002 12:32
Hi Gregg,
On Saturday, May 25, 2002, 5:37:55 AM, you wrote:
(CC to Gregg and Luis to avoid list slowness)
GI> Anyone else listening in? Any thoughts?
Sorry for using a nonstandard style... You should be easily able
to fix it for negative numbers (I didn't need them :) and to use
different decimal separators (this is for the italian way).
form-decimal:
func
[ num [number!]
cifre [integer!]
/local str poscifre
]
[ ; ***WARNING*** positive numbers only.
num: abs num
str: make string! 16
either zero? num
[ insert str #"0"
if cifre > 0
[ insert/dup
insert
tail str
#","
#"0"
cifre
] ]
[ if 14 < add cifre log-10 num
[ return form num]
num: form
add
multiply
power 10 cifre
to-decimal num
0,5
clear any [find num "." ""]
poscifre: skip tail num negate cifre
insert/part str num
num: skip num
1 + remainder
subtract
index? poscifre
2
3
while [(index? poscifre) > (index? num)]
[ insert/part
insert
tail str
#"'"
num
num: skip num 3
]
if empty? str [insert str #"0"]
if not tail? poscifre
[ insert
insert/dup
insert
tail str
#","
#"0"
cifre - length? poscifre
poscifre
]
]
str
]
>> form-decimal 1 2
== "1,00"
>> form-decimal 1000 2
== "1'000,00"
>> form-decimal 0.1 2
== "0,10"
>> form-decimal 0.01 2
== "0,01"
>> form-decimal 0.01 5
== "0,01000"
>> form-decimal 0.00001 5
== "0,00001"
>> d: .0659262575231111
== 6.59262575231111E-2
>> form-decimal d 5
== "0,06593"
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r