By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,789 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

Re: Getting fractional part from a float without using stringoperations

P: n/a
On Wed, Nov 19, 2008 at 10:01 PM, srinivasan srinivas
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 <ti**>
To: srinivasan srinivas <sr*********>
Cc: Jeremiah Dodds <je************>; py*********
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
for the problems with FP figures.


Get perfect Email ID for your Resume. Grab now
Nov 20 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.