[REBOL] Re: Translate this code into real Rebol?
From: g:santilli:tiscalinet:it at: 23-Jan-2002 22:32
Hello [SunandaDH--aol--com]!
On 23-Gen-02, you wrote:
S> works, but it could almost be Basic. Is there a more Rebolish
S> (Rebellious? Rebvolting?) way of doing this?
I don't know if this is more REBOLish, and it does not have all
the features of your version, but the output can be loaded by
REBOL and I've been using it for a lot of time.
>> form-decimal 123 none 2
== "123,00"
>> form-decimal 12345 none 2
== "12'345,00"
>> form-decimal 12345678 none 0
== "12'345'678"
>> form-decimal 123 10 2
== " 123,00"
>> form-decimal 123456 10 2
== "123'456,00"
right: func [str [string!] n [integer!]] [
head insert/part insert/dup make string! n #" " n - length? str tail str negate n
]
form-decimal: func [num [number!] len [integer! none!] cifre [integer!] /local str num-len]
[
; ***WARNING*** positive numbers only.
num: abs num
str: make string! len
either zero? num [
num-len: 1
if cifre > 0 [num-len: num-len + cifre + 1]
len: any [len num-len]
insert insert/dup str #" " subtract len num-len #"0"
if cifre > 0 [insert/dup insert tail str #"," #"0" cifre]
] [
if 14 < add cifre log-10 num [return either len [right form num len] [form num]]
num: form add multiply power 10 cifre to-decimal num 0,5
clear any [find num "." ""]
; for numbers < 1
insert/dup num #"0" 1 + cifre - length? num
cifre: skip tail num negate cifre
num-len: to-integer divide subtract index? cifre 2 3
num-len: add num-len length? num
if not tail? cifre [num-len: add num-len 1]
len: any [len num-len]
insert/part
insert/dup str #" " subtract len num-len
num
num: skip num add 1 (subtract index? cifre 2) // 3
while [(index? cifre) > (index? num)] [
insert/part insert tail str #"'" num num: skip num 3
]
if not tail? cifre [
insert insert tail str #"," cifre
]
]
str
]
Regards,
Gabriele.
--
Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer
Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/