[Python-Dev] Rounding float to int directly (Re: struct module and coercing floats to integers)

M.-A. Lemburg mal at egenix.com
Tue Aug 1 13:40:06 CEST 2006


Greg Ewing wrote:
> M.-A. Lemburg wrote:
> 
>> I suppose you don't know about the optional argument
>> to round that lets you round up to a certain decimal ?!
> 
> Yes, I know about, but I rarely if ever use it.
> Rounding a binary float to a number of decimal
> places seems a fundamentally ill-considered thing
> to do anyway. What are the use cases for it,
> given that one can easily select a number of decimal
> places when formatting a number for display?

You often have a need for controlled rounding when doing
financial calculations or in situations where you want to
compare two floats with a given accuracy, e.g. to work
around rounding problems ;-)

The usual approach is to use full float accuracy
throughout the calculation and then apply rounding
a certain key places.

Float formatting is an entirely different issue.

>> If we were to follow your suggestion, we'd have round()
>> return an integer when called without the argument and
>> a float when called with the argument.
> 
> No, round() wouldn't have that option at all. If
> you wanted it, you would use fround() instead,
> which would have the option and return a float
> always.
> 
> This would be a Py3k thing, obviously. If done
> before then, the new function would have to be
> given a different name.

Hmm, looks like a YAGNI to me, but perhaps I'm missing
something :-)

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Aug 01 2006)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::


More information about the Python-Dev mailing list