[REBOL] Re: Limiting Precision of Decimals,
From: rpgwriter:yah:oo at: 25-May-2002 0:10
--- Gregg Irwin <[greggirwin--mindspring--com]> wrote:
> Hi Louis,
>
> << Thanks! But I haven't been able to figure out
> how to use it. Would you
> please give me a sample usage? >>
>
> DOH! I was going to post a simple reply, but it
> appears you've outsmarted
> me. :) If there's only a decimal component, and the
> value is <~.1, REBOL
> returns it with scientific notation. E.g.
>
> >> round/places/truncate 1.0659262575231111 8
> == 1.06592625
> >> round/places/truncate .0659262575231111 8
> == 6.592625E-2
>
> I'll have to think about this. I'm not sure that I
> want to scale things
> internally to work around this issue. Obviously, it
> should work for all
> extremes so that's what we'll have to test for in a
> redesign. Guess it won't
> work for you right now though. Sorry about that.
> But, thanks for finding the
> bug! :)
>
> Anyone else listening in? Any thoughts?
There's not a bug in your rounding function. As you
note, its just a "feature" of the way REBOL converts
decimal! values to string! values. There is probably a
place for a nice robust function for converting
arbitrary values of any type to strings with all kinds
of formatting options (sort of an sprintf() for
REBOL), but the direct solution to printing decimals
to avoid scientific notation for small values is:
print-val: either greater? value 0.1 [ to-string value
] [ insert remove to-string 1 + value "0" ]
This still has the switch over to scientific notation
at values of >= 1E+15. To totally avoid scientific
notation, this seems to work (written as a function):
dec-to-string: func [ value ] [
z: value
w: ""
for y to-integer log-10 z 0 -1 [
append w to-string to-integer z / power 10 y
z: remainder z power 10 y
]
append w remove to-string 1 + remainder z 1
return w
]
Chris Dicely