Change from an integer to a decimal

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sushi
    New Member
    • Dec 2006
    • 18

    Change from an integer to a decimal

    Heyo all,

    Having a problem which seems really stupid but I just cant solve it.

    Im reading in a number of integers from a file e.g. 179, 23, 56
    I then have to divde them by 255, but when I do I only ever get values of 0 or 1 rather than something like 0.56, 0.79. I thought I could just convert the number to a float before dividing it e.g a = 179 therefore float(a), but that doesnt seem to work, and Ive looked about on the web but cant find an answer.

    Any help would be most appreciated thanks!
    Mel
  • dshimer
    Recognized Expert New Member
    • Dec 2006
    • 136

    #2
    float() one or both of the numbers before the division, or just use a real for the denominator. If either number is a real, then the answer will be a real

    Code:
    >>> 255/178
    1
    >>> 255/178.0
    1.4325842696629214
    >>> float(255)/178
    1.4325842696629214
    >>>

    Comment

    • ghostdog74
      Recognized Expert Contributor
      • Apr 2006
      • 511

      #3
      you can also run your script like this
      Code:
       python -Qnew yourscript.py
      or in your script, import __future__

      Code:
      from __future__ import division
      print 255/178

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by ghostdog74
        you can also run your script like this
        Code:
         python -Qnew yourscript.py
        or in your script, import __future__

        Code:
        from __future__ import division
        print 255/178
        This is new to me so I looked it up. Presently the '/' operator is mapped to object method __div__(). In the future it will be remapped to __truediv__(), which evaluates integer division to float.

        __truediv__() can also be accessed thus:
        Code:
        >>> from operator import truediv
        >>> truediv(1,2)
        0.5
        >>> truediv(21,7)
        3.0
        >>>
        Importing division enables this feature.

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Sushi: Great to have you back after so long.
          You other three: You guys are awesome. Great posts!

          Comment

          • Sushi
            New Member
            • Dec 2006
            • 18

            #6
            Thank you all very much for your help!
            I wasn't floating both numbers, only the ones that were being read in from the file which is why it wasnt working. So again many thanks for the help!

            Barton: Good to be back! I've been working on using Blender quite a lot, and whilst it's a very nice piece of software isnt exactly the easiest thing to use with python!

            Mel

            Comment

            Working...