ROUNDING???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • katie smith

    ROUNDING???

    in python im doing the problem 255/494

    it keeps giving me 0 instead of .51....
    what am i doing wrong?

    please help me I have been looking for hours


    _______________ _______________ _______________ _______________ _______________ _________
    Never miss a thing. Make Yahoo your home page.


  • 7stud

    #2
    Re: ROUNDING???

    On Feb 18, 7:57 pm, katie smith <iceboy...@yaho o.comwrote:
    in python im doing the problem 255/494
    >
    it keeps giving me 0 instead of .51....
    what am i doing wrong?
    >
    please help me I have been looking for hours
    >
          _______________ _______________ _______________ _______________ _______________ _________
    Never miss a thing.  Make Yahoo your home page.http://www.yahoo.com/r/hs
    An integer divided by an integer produces an integer. In computer
    programming, that's called 'integer arithmetic', and any fractional
    part of the result is chopped off(not rounded). If your arithmetic
    involves at least one float, then you will get a float as an asnwer:


    print 255/494
    print 255.0/494
    print (255 * 1.0)/494

    --output:--
    0
    0.516194331984
    0.516194331984

    Comment

    • Jeff Schwab

      #3
      Re: ROUNDING???

      7stud wrote:
      On Feb 18, 7:57 pm, katie smith <iceboy...@yaho o.comwrote:
      >in python im doing the problem 255/494
      >>
      >it keeps giving me 0 instead of .51....
      >what am i doing wrong?
      >>
      >please help me I have been looking for hours
      >>
      > _______________ _______________ _______________ _______________ _______________ _________
      >Never miss a thing. Make Yahoo your home page.http://www.yahoo.com/r/hs
      >
      An integer divided by an integer produces an integer. In computer
      programming, that's called 'integer arithmetic', and any fractional
      part of the result is chopped off(not rounded). If your arithmetic
      involves at least one float, then you will get a float as an asnwer:
      >
      >
      print 255/494
      print 255.0/494
      print (255 * 1.0)/494
      >
      --output:--
      0
      0.516194331984
      0.516194331984
      Not that this behavior is expected to change in the future, such that
      255 / 494 will actually perform floating-point division. The way to
      achieve flooring division will be 255 // 494.

      Comment

      • Aahz

        #4
        Re: ROUNDING???

        In article <mailman.949.12 03389866.9267.p ython-list@python.org >,
        katie smith <iceboy127@yaho o.comwrote:
        >
        >in python im doing the problem 255/494
        >
        >it keeps giving me 0 instead of .51....
        >what am i doing wrong?
        >>from __future__ import division
        >>2/5
        0.4000000000000 0002

        In addition:
        >>division
        _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)

        Therefore this works in Python 2.2 and higher.
        --
        Aahz (aahz@pythoncra ft.com) <* http://www.pythoncraft.com/

        "All problems in computer science can be solved by another level of
        indirection." --Butler Lampson

        Comment

        • subeen

          #5
          Re: ROUNDING???

          You can use the round() function. And if you want to print, use %0.2f


          regards,
          Subeen.



          On Feb 19, 9:36 am, a...@pythoncraf t.com (Aahz) wrote:
          In article <mailman.949.12 03389866.9267.p ython-l...@python.org >,
          katie smith <iceboy...@yaho o.comwrote:
          >
          >
          >
          in python im doing the problem 255/494
          >
          it keeps giving me 0 instead of .51....
          what am i doing wrong?
          >from __future__ import division
          >2/5
          >
          0.4000000000000 0002
          >
          In addition:
          >
          >division
          >
          _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)
          >
          Therefore this works in Python 2.2 and higher.
          --
          Aahz (a...@pythoncra ft.com) <* http://www.pythoncraft.com/
          >
          "All problems in computer science can be solved by another level of
          indirection." --Butler Lampson

          Comment

          • Asun Friere

            #6
            Re: ROUNDING???

            On Feb 19, 2:05 pm, 7stud <bbxx789_0...@y ahoo.comwrote:
            An integer divided by an integer produces an integer. In computer
            programming, that's called 'integer arithmetic', and any fractional
            part of the result is chopped off(not rounded).
            In case you care, the "chopped off" bit is given by the modulo
            operator '%'. So integer division x/y is really like the everyday y
            goes into x, p times, remainder q, for example:
            >>10/3, 10%3
            (3, 1)
            If your arithmetic
            involves at least one float, then you will get a float as an asnwer:
            >
            print 255/494
            print 255.0/494
            print (255 * 1.0)/494
            or indeed "print float(255)/494"

            Comment

            Working...