Re: Decimal Point Truncations?
Re: Decimal Point Truncations?
- Subject: Re: Decimal Point Truncations?
- From: email@hidden
- Date: Fri, 9 Feb 2001 11:25:09 EST
In a message dated 2/8/01 12:50:35 PM, Greg Smith wrote:
I'm having a problem truncating decimals. Can anyone help?
-- snip all the other interesting answers because they've gotten so long --
As a self appointed Fr.D. (Doctor of Frustration) in dealing with this
subject, allow me to share some points, and my own handler for this issue.
1) Truncation is not rounding. 2.9999 rounds to 3 but truncates to 2.99
(assuming we want to truncate to 2 decimal places).
2) Truncation is not formatting. Hence, 3.1 dollars formats to $ 3.10, but
truncates to 3.1 (no dollar sign, no trailing padded zero). Padding a
truncated result goes beyond truncating.
3) Truncation is not math. The result of a truncation should be a string. The
result of rounding is a number.
3a) Working with numbers as strings is slower, in many cases, than working
with numbers as numbers (but sometimes necessary).
3b) Coercing between strings and numbers is slower still (but sometimes
necessary).
The question comes down to, "do we want 9.099 returned as 9 or 9.00 or 9.1 or
9.09 or 9.10?"
Having said that, it is highly unlikely that any "one size fits all" number
formatting routine will be the correct choice for everyone, every time.
Issues to consider:
1) Does speed matter? If the routine is only called once or twice in a
babysitting situation, probably not. If it is called thousands of times in a
CGI, it is extremely important.
2) Does precision matter? If formatting dollars, probably not (ie. it's
always 2 decimal places). A more specific routine may be shorter, faster, and
easier to work with.
3) Are we producing a number to work with further? If so, rounding is
probably the better choice.
4) Are we padding with zeros?
5) Are we expecting string or numeric input?
OK. Here's my dollar formatting routine, battle tested as fast and reliable,
accepting numbers or strings.
on formatDollars(price)
set price to price as number
if price mod 1 = 0 then return "" & (price div 1) & ".00"
if (price * 10) mod 1 < 0.1 then return "" & (((price * 10) div 1) / 10)
& "0"
return (((price * 100) div 1) / 100) as string
end formatDollars
HTH.
Jeff Baumann
email@hidden
www.linkedresources.com
History is interesting. An apple fell on Newton's head and he invented
Calculus, while Bill Gates invented Windows and got a pie in the face.