Getting fractional part from a float without using string operations

John Machin sjmachin at lexicon.net
Wed Nov 19 16:37:18 EST 2008


On Nov 20, 3:38 am, "Blind Anagram" <nob... at nowhere.org> wrote:
> "MRAB" <goo... at mrabarnett.plus.com> wrote in message
>
> news:278930e8-6c65-4059-9dec-713e4ba5b554 at w1g2000prk.googlegroups.com...
> On Nov 19, 1:44 pm, John Machin <sjmac... at lexicon.net> wrote:
>
> > On Nov 20, 12:35 am, srinivasan srinivas <sri_anna... at yahoo.co.in>
> > wrote:
> > <null>
>
> > | >>> import math
> > | >>> num = 123.4567
> > | >>> math.modf(num)
> > | (0.45669999999999789, 123.0)
>
> def frac(n):
>     return n - int(n)
>
> n % 1.0

| >>> n= -123.4567
| >>> n % 1.0
| 0.54330000000000211



More information about the Python-list mailing list