Getting fractional part from a float without using string operations
clp at rebertia.com
Thu Nov 20 07:24:03 CET 2008
On Wed, Nov 19, 2008 at 10:01 PM, srinivasan srinivas
<sri_annauni at yahoo.co.in> wrote:
> Yes it works for most of the cases. But it doesn't for the following case:
Since you really care about significant figures here, have you
considered using decimal rather than float as, IIRC, it handles this
Follow the path of the Iguana...
> ----- Original Message ----
> From: Tino Wildenhain <tino at wildenhain.de>
> To: srinivasan srinivas <sri_annauni at yahoo.co.in>
> Cc: Jeremiah Dodds <jeremiah.dodds at gmail.com>; python-list at python.org
> Sent: Wednesday, 19 November, 2008 7:33:46 PM
> Subject: Re: Getting fractional part from a float without using string operations
> srinivasan srinivas wrote:
>> Yes. But it didn't give only the expected decimals.
>> For ex:
>> >>> a = 1.23
>> >>> abs(int(a) -a)
>> I would like to get the result '0.23' only.
> well, thats what get stored internally - there
> is no way around it if you are using floating
> point numbers:
> but str() handles the rounding correctly:
>>>> print 0.23
>>>> print abs(int(a) -a)
> See also http://en.wikipedia.org/wiki/Floating_point
> for the problems with FP figures.
> Get perfect Email ID for your Resume. Grab now http://in.promos.yahoo.com/address
More information about the Python-list