[Tutor] rounding up to the nearest penny

Andrei Kulakov ak@silmarill.org
Wed, 03 Oct 2001 15:17:41 -0400


On Wed, Oct 03, 2001 at 08:25:17AM -0500, Timothy Wilson wrote:
> Hi everyone,
> 
> I gave my students a programmig assignment (see
> http://www.isd197.org/sibley/cs/icp/assignments/loancalc_html for
> specifics) and discovered one small feature that I couldn't figure out (at
> least easily).
> 
> My impression is that when doing financial calculations dollar amounts are
> often rounded up to the nearest penny. The round function does a true
> round to the nearest penny. The ceil function doesn't appear to work with
> floats.
> 
> >>>
> >>> amount = 1.342
> >>> round(amount, 2)
> 1.34
> >>> import math
> >>> math.ceil(amount)
> 2.0
> 
> Any suggestions?
> 
> -Tim

Since this question was answered already, I'll comment on usage of floats
for currency - this is frowned upon! Floats are inexact. Currency is
something you want to be exact with :P. Instead, as far as I understand,
people use integers - they convert the whole sum to pennies and assign it
to an integer or have two integers, for pennies and dollars.

> 
> --
> Tim Wilson      |   Visit Sibley online:   | Check out:
> Henry Sibley HS |  http://www.isd197.org   | http://www.zope.com
> W. St. Paul, MN |                          | http://slashdot.org
> wilson@visi.com |  <dtml-var pithy_quote>  | http://linux.com
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org