Best way to calculate fraction part of x?
Russell Owen
rowen at uw.edu
Thu Mar 26 18:02:56 EDT 2015
On 3/24/15 6:39 PM, Jason Swails wrote:
>
>
> On Mon, Mar 23, 2015 at 8:38 PM, Emile van Sebille <emile at fenx.com
> <mailto:emile at fenx.com>> wrote:
>
> On 3/23/2015 5:52 AM, Steven D'Aprano wrote:
>
> Are there any other, possibly better, ways to calculate the
> fractional part
> of a number?
>
>
> float (("%6.3f" % x)[-4:])
>
>
> In general you lose a lot of precision this way...
I suggest modf in the math library:
math.modf(x)
Return the fractional and integer parts of x. Both results carry the
sign of x and are floats.
More information about the Python-list
mailing list