[BangPypers] Need Help : Setting Floating Precision Point as 2 in Python

Arulalan T arulalant at gmail.com
Tue Aug 24 13:42:05 CEST 2010


2010/8/24 Noufal Ibrahim <noufal at gmail.com>

>
> (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").
>
>
I am not just printing the float value.


> 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'
>

This is wrong ans.

>>> str(a+b)
'80'.

i.e. '80' only, not '80.23'.

And I dont want this in string. I need only in float value, that too exactly
in precision 2.




>
> [...]
>
> Can you tell us *why* this inaccuracy is hurting you?
>
>
I am doing project for Indian Meteorological Department using CDAT python
library.

Now I am plotting weather symbol markers on India Map at corresponding
latitude and longitude (in float) dynamically.
Due to inaccuracy floating value of latitude & logitude,
the position of markers are moved away from the exact position of the
stations on the map.

so I need float value in 2 precision without changing the value. Not in
string.

Any suggestions?

Thanks


>
> --
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
Regards,
Arulalan.T

Kanchi Linux User Group Rocks !
http://kanchilug.wordpress.com

My Experiments In Linux are here
http://tuxcoder.wordpress.com


More information about the BangPypers mailing list