Re: Decimal Point Truncations?
Re: Decimal Point Truncations?
- Subject: Re: Decimal Point Truncations?
- From: Nigel Garvey <email@hidden>
- Date: Fri, 9 Feb 2001 00:37:33 +0000
Paul Berkowitz wrote on Thu, 08 Feb 2001 11:22:54 -0800:
>
On 2/8/01 10:45 AM, "Jed Verity" <email@hidden> wrote:
>
>
> Hello Greg,
>
>
>
> Glad it works. I can't say this is the standard method for truncating (I
>
> hope it isn't, quite frankly) but I couldn't find anything in the standard
>
> additions that would truncate without converting the class from real to
>
> string. The rounding function (the only promising one) requires an integer
>
> result. I'm sure there's an OSAX that would do this but it hardly seems
>
> worth it...
>
>
>
> Anyone else have insight into this one?
>
>
>
Nigel Garvey has come up with some wonderful handlers, about 2 or 3 weeks
>
ago. Check the archives here and at MacScrpt, or he might send them again.
>
(The archives for this list aren't searchable, so you'd have to dig your way
>
back chronologically -- terrible.) Or maybe Nigel will send them again.
Thanks, Paul. :-) I like Jed's idea. It's crude but effective - provided
that the number coerced to string isn't in scientific format. (This is
currently probably unlikely in the "gigabyte" situation under discussion.)
The truncation handler I posted on Jan 28th looks like this. It's more to
type than other solutions, but it's fast and flexible as part of a
library. It's from a set I wrote last year out of annoyance with 'round'
and as an excercise in its own right. I keep meaning to submit it (the
set) to macscripter.net's ScriptBuilders archive, but haven't got round
to it yet.
(* Truncate n to d decimal places, with or without rounding. *)
on trnc of n at d given rounding:rounding
tell 10 ^ d
tell n * it
if rounding then
it div 0.5 - it div 1
else
it div 1
end if
end tell
result / it -- ie. result / (10 ^ d)
end tell
end trnc
trnc of 54.646464674878 at 1 with rounding
--> 54.6
NG