Re: fractional cents

"A. Pagaltzis" <[email protected]>
Newsgroups gmane.comp.lang.perl.fun
Message-ID <20040421222712.GC5619@klangraum>
* Bernie Cosell <[email protected]> [2004-04-22 00:02]:
> On 21 Apr 2004 at 22:55, A. Pagaltzis wrote:
> > If you do this by looking at $amt, then your method must be
> > mathematical, because chopping characters in the string
> > representation of the unrounded $amt might occasionally lead to
> > results different from what the mathematical method would have
> > produced with rounding involved.
> 
> Correct, but doesn't "%.Xf" round?  And so running it through
> the (s)printf before you mess with it gets you the desired
> rounding, doesn't it?

Yes, that's what I said further down in my mail.

The point is that using something like $amt =~ /\.\d\d\d/ will
probably lead to inconsistencies because $amt is unrounded, while
printf then produces a rounded form that depends on at least one
more digit, probably also on whether the non-fractional part is
odd or even, and a number of other factors.

So if your conditional refers to $amt, it should use math, like
($amt * 1000) % 10.

You *can* use string mangling, however, if you mangle the
*result*.

> When you're pricing things, prices are in dollars and cents....
> except for [...]

I know what he wanted and why, and wasn't even saying anything
about that.

-- 
Regards,
Aristotle
 
"If you can't laugh at yourself, you don't take life seriously enough."
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.