2010/8/24 Noufal Ibrahim <nou...@gmail.com>

> Arulalan T <arulal...@gmail.com> writes:
>
> [...]
>
> >> 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.
>
> I'm quite sure that you're doing more than printing. I'm asking you
> whether the 2 decimal places business applies at all levels of the
> program or only at the display level.
>
> >> >>> import decimal
> >> >>> a = decimal.Decimal("79.73")
> >> >>> b = decimal.Decimal("0.5")
> >>
> >
> >
> >> >>> str(a + b)
> >>
> >
> >
> >> '80.23'
> >>
> >
> > This is wrong ans.
>
>
> 79.73 + 0.5 is 80.23.
>
> Why is this a wrong answer?
>
>
> >>>> 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.
>
> That's just the string representation. You got it because you called
> "str" on the value.
>
> >>> import decimal
> >>> a = decimal.Decimal("79.73")
> >>> b = decimal.Decimal("0.5")
> >>> a + b
> Decimal('80.23')
>
>
> [...]
>
> > 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?
>
> [...]
>
> Well, you shouldn't be using floating point numbers to represent things
> like currency, longitude/latitute etc.
>
> What is the problem with the decimal module?
>
> You can fine tune the precision of your current execution thread using
> the getcontext method
>
> >>> import decimal
> >>> decimal.getcontext().prec = 2            # Two decimal points
> >>> decimal.Decimal(1) / decimal.Decimal(7)
> Decimal('0.14')
> >>> decimal.getcontext().prec = 15           # 15 decimal points
> >>> decimal.Decimal(1) / decimal.Decimal(7)
> Decimal('0.142857142857143')
> >>>
>
>
>


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

In CDAT vcs module supports only the 'float data' type to represent the
latitude & logitude in map.

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



Any suggestions?





>
>
>
>
> --
> _______________________________________________
> BangPypers mailing list
> BangPypers@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
_______________________________________________
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers

Reply via email to