Remove integer from float number

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Derek Basch

    #1

    Remove integer from float number

    How can I return:

    ".666"

    from float:

    "0.666"

    This is what I have so far:
    [color=blue][color=green][color=darkred]
    >>> "%.6f" % x[/color][/color][/color]

    Thanks Everyone,
    Derek Basch

  • Larry Bates

    #2
    Re: Remove integer from float number

    Derek Basch wrote:[color=blue]
    > How can I return:
    >
    > ".666"
    >
    > from float:
    >
    > "0.666"
    >
    > This is what I have so far:
    >[color=green][color=darkred]
    >>>> "%.6f" % x[/color][/color]
    >
    > Thanks Everyone,
    > Derek Basch
    >[/color]

    This works but I'm not entirely sure I know what you are
    trying to accomplish.

    ("%.3f" % x)[1:]


    -Larry Bates

    Comment

    • Derek Basch

      #3
      Re: Remove integer from float number

      Ahh yes you have to put parenthases around the string formatting to
      remove the integer using indexes. Thanks, that's just what I needed!

      Derek Basch

      Comment

      • Paul Rubin

        #4
        Re: Remove integer from float number

        "Derek Basch" <dbasch@yahoo.c om> writes:[color=blue]
        > Ahh yes you have to put parenthases around the string formatting to
        > remove the integer using indexes. Thanks, that's just what I needed![/color]

        I think it's better to remove leading zeros explicitly:

        ('%.3x' % x).lstrip('0')

        Comment

        • John Machin

          #5
          Re: Remove integer from float number

          On 24/03/2006 6:44 AM, Larry Bates wrote:[color=blue]
          > Derek Basch wrote:
          >[color=green]
          >>How can I return:
          >>
          >>".666"
          >>
          >>from float:
          >>
          >>"0.666"
          >>
          >>This is what I have so far:
          >>
          >>[color=darkred]
          >>>>>"%.6f" % x[/color]
          >>
          >>Thanks Everyone,
          >>Derek Basch
          >>[/color]
          >
          >
          > This works but I'm not entirely sure I know what you are
          > trying to accomplish.
          >
          > ("%.3f" % x)[1:]
          >[/color]
          [color=blue][color=green][color=darkred]
          >>> x = 12345.666; ("%.3f" % x)[1:][/color][/color][/color]
          '2345.666'[color=blue][color=green][color=darkred]
          >>>[/color][/color][/color]

          I'm sure of neither what the OP is trying to accomplish nor what Larry's
          definition of "works" is :-)

          Perhaps the condition abs(x) < 1.0 is implied ...

          Comment

          • Larry Bates

            #6
            Re: Remove integer from float number

            John Machin wrote:[color=blue]
            > On 24/03/2006 6:44 AM, Larry Bates wrote:[color=green]
            >> Derek Basch wrote:
            >>[color=darkred]
            >>> How can I return:
            >>>
            >>> ".666"
            >>>
            >>> from float:
            >>>
            >>> "0.666"
            >>>
            >>> This is what I have so far:
            >>>
            >>>
            >>>>>> "%.6f" % x
            >>>
            >>> Thanks Everyone,
            >>> Derek Basch
            >>>[/color]
            >>
            >>
            >> This works but I'm not entirely sure I know what you are
            >> trying to accomplish.
            >>
            >> ("%.3f" % x)[1:]
            >>[/color]
            >[color=green][color=darkred]
            >>>> x = 12345.666; ("%.3f" % x)[1:][/color][/color]
            > '2345.666'[color=green][color=darkred]
            >>>>[/color][/color]
            >
            > I'm sure of neither what the OP is trying to accomplish nor what Larry's
            > definition of "works" is :-)
            >
            > Perhaps the condition abs(x) < 1.0 is implied ...[/color]

            For the example given, my code works. With so little information
            the only thing I could do is answer the specific question and
            caveat it that I don't know "exactly" what OP is trying to accomplish.
            By the OPs response to my post, it was what he was looking for.
            But I agree it is very much an edge-case question.

            -Larry Bates

            Comment

            • Arne Ludwig

              #7
              Re: Remove integer from float number

              With that terse description and the subject line I would interpret the
              OP like so:
              [color=blue][color=green][color=darkred]
              >>> print re.sub(".*\."," .","0.666")[/color][/color][/color]
              ..666[color=blue][color=green][color=darkred]
              >>> print re.sub(".*\."," .","123.666" )[/color][/color][/color]
              ..666

              Comment

              • Grant Edwards

                #8
                Re: Remove integer from float number

                On 2006-03-23, Arne Ludwig <arne@citde.net > wrote:[color=blue]
                > With that terse description and the subject line I would interpret the
                > OP like so:
                >[color=green][color=darkred]
                >>>> print re.sub(".*\."," .","0.666")[/color][/color]
                > .666[color=green][color=darkred]
                >>>> print re.sub(".*\."," .","123.666" )[/color][/color]
                > .666[/color]

                Or if you're allergic to regular expressions:
                [color=blue][color=green][color=darkred]
                >>> print "." + "0.666".split(" .")[-1][/color][/color][/color]
                ..666[color=blue][color=green][color=darkred]
                >>> print "." + "123.666".split (".")[-1][/color][/color][/color]
                ..666[color=blue][color=green][color=darkred]
                >>>[/color][/color][/color]



                --
                Grant Edwards grante Yow! Yow! It's a hole
                at all the way to downtown
                visi.com Burbank!

                Comment

                Working...