2010/8/24 Chandrashekar Babu <[email protected]>
> Hi Arulalan,
>
>
> > 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.
> >
> > >>>a=79.73
> > >>>b=0.5
> > >>> a+b
> > 80.230000000000004
> >
> > I need exactly 2 precision point in this float value. i.e. 80.23
> >
>
> You might want to convert them to Decimal type. Look up the
> following URL (Python documentation) for more information:
> http://docs.python.org/library/decimal.html
>
>>> from decimal import *
>>> getcontext().prec = 3
>>> getcontext().rounding = ROUND_UP
>>> Decimal('3.1415926535') + Decimal('2.7182818285')
Decimal('5.86')
or
>>> getcontext().prec = 3
>>> getcontext().rounding = ROUND_DOWN
>>> Decimal('3.1415926535') + Decimal('2.7182818285')
Decimal('5.85')
This gives the what I need.
But I can not use this Decimal data type.
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 in vcs.
so I need float value in 2 precision without changing the value. Not in
string. Not in Decimal.
In CDAT vcs module supports only the 'float data' type to represent the
latitude & logitude in map.
Any suggestions?
>
> If all you want to do is "print" the value upto 2 decimal points, then
> a print with format specifier should do the trick. The following URL
> (Stack Overflow) has this question answered:
>
>
> http://stackoverflow.com/questions/455612/python-limiting-floats-to-two-decimal-points
>
> Cheers,
> Chandrashekar.
>
> --
> Chandrashekar Babu.,
> http://www.chandrashekar.info/
> _______________________________________________
> ILUGC Mailing List:
> http://www.ae.iitm.ac.in/mailman/listinfo/ilugc
>
--
Regards,
Arulalan.T
Kanchi Linux User Group Rocks !
http://kanchilug.wordpress.com
My Experiments In Linux are here
http://tuxcoder.wordpress.com
_______________________________________________
ILUGC Mailing List:
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc