[BangPypers] Need Help : Setting Floating Precision Point as 2 in Python
Noufal Ibrahim
noufal at gmail.com
Tue Aug 24 13:31:59 CEST 2010
(I posted this to ChennaiPy but this list is wider so I'm reposting
it).
Arulalan T <arulalant at gmail.com> writes:
> Dear All,
>
> I need one help.
>
> In python, I need to set the floating point precision as 2.
>
> I am getting the following output in python while adding two float nos.
Often, you *need* to only display the result of your calculations with n
decimal places. Is yours such a situation? If so, forget the number of
decimal points during the computation and when printing format it as
appropriate ("%.2f").
If you genuinely want to work without the floating point problem that
you've indicated in your email, consider using the decimal module
>>> import decimal
>>> a = decimal.Decimal("79.73")
>>> b = decimal.Decimal("0.5")
>>> str(a + b)
'80.23'
[...]
Can you tell us *why* this inaccuracy is hurting you?
--
More information about the BangPypers
mailing list