[Tutor] round() yields in-expected results/commercial rounding

Daniel Ehrenberg littledanehren at yahoo.com
Sat Dec 13 11:57:51 EST 2003


Harm_Kirchhoff at mail.digital.co.jp wrote:
> I have the following problem:
> 
> >>> float(8.7)
> 8.6999999999999993
> >>> float(870)/100
> 8.6999999999999993
> >>> 8.7 * 100
> 869.99999999999989
> >>> round(8.7,2)
> 8.6999999999999993
> >>> math.modf(8.7)
> (0.69999999999999929, 8.0)
> >>> int(float(8.7*100))
> 869
> 
> especially anoying is:
> >>> n = float(8.7*100) ; print n
> 870.0
> >>> n
> 869.99999999999989
> >>> 
> 
> How can I ensure that 8.7 stays 8.7, so that
> 8.7 * 100 = 870 and not 869.9999...
> 
> The way I came up with is:
> >>> f = float(8.7*100)   ;  i = int(f)   ; i,f
> (869, 869.99999999999989)
> >>> if f>=(i+.5): i += 1 ;  i
> 
> 870
> 
> I am writing a commercial sales database. To save
> memory I use integers 
> internally, rather than floats.
> This means that an amount of 8.70 is converted to
> 870 internally.
> if it is converted to 869 I loose 1/10.

This is annoying, but the only way short of using a
library to perform this simple task that I can thing
of is to convert the float into a string and then into
an integer. Here's a simple function that does that:

>>> convert = lambda x: int(str(x*100)[:-2])
>>> convert(8.7)
870

You need the [:-2] to truncate the last two letters
because otherwise it would be in the form '870.0',
which it can't handle.

Daniel Ehrenberg

__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/



More information about the Tutor mailing list