How to show percentage

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sen-Lung Chen

    #1

    How to show percentage

    Dear All:
    I have a question of show percentage.
    For example ,I want to show the percentage of 1/3 = 33.33%

    I use the 1*100/3 = 33
    it is 33 not 33.33 , how to show the 33.33 %
    Thanks

  • dylan.moreland@gmail.com

    #2
    Re: How to show percentage

    Traditionally, one part of the expression has to be a float for the
    result to be a float (this is a holdover from C). So 100/3.0 will give
    you the result you want. Alternatively, you can put "from __future__
    import division" at the top of your script, and then 100/3 will return
    a float.



    Comment

    • Bill Mill

      #3
      Re: How to show percentage

      You need to convert 1 or 3 to a float. How about:
      [color=blue][color=green][color=darkred]
      >>> def pct(num, den): return (float(num)/den) * 100[/color][/color][/color]
      ....[color=blue][color=green][color=darkred]
      >>> pct(1, 3)[/color][/color][/color]
      33.333333333333 329

      Peace
      Bill Mill
      bill.mill at gmail.com

      On 22 Sep 2005 10:51:43 -0700, Sen-Lung Chen
      <slchen@slugger .ee.nthu.edu.tw > wrote:[color=blue]
      > Dear All:
      > I have a question of show percentage.
      > For example ,I want to show the percentage of 1/3 = 33.33%
      >
      > I use the 1*100/3 = 33
      > it is 33 not 33.33 , how to show the 33.33 %
      > Thanks
      >
      > --
      > http://mail.python.org/mailman/listinfo/python-list
      >[/color]

      Comment

      • Mike Meyer

        #4
        Re: How to show percentage

        "Sen-Lung Chen" <slchen@larc.ee .nthu.edu.tw> writes:
        [color=blue]
        > Dear All:
        > I have a question of show percentage.
        > For example ,I want to show the percentage of 1/3 = 33.33%
        >
        > I use the 1*100/3 = 33
        > it is 33 not 33.33 , how to show the 33.33 %[/color]

        Python interprets '/' in an integer environment to return ints, not
        floats. You can either turn one of your arguments into floats:
        [color=blue][color=green][color=darkred]
        >>> float(1 * 100) / 3[/color][/color][/color]
        33.333333333333 336

        This is going to change. Not sure when. You can ask for the new behavior:
        [color=blue][color=green][color=darkred]
        >>> from __future__ import division
        >>> 100 / 3[/color][/color][/color]
        33.333333333333 336

        <mike

        --
        Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
        Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

        Comment

        • George Sakkis

          #5
          Re: How to show percentage

          "Sen-Lung Chen" <slchen@larc.ee .nthu.edu.tw> wrote:[color=blue]
          > Dear All:
          > I have a question of show percentage.
          > For example ,I want to show the percentage of 1/3 = 33.33%
          >
          > I use the 1*100/3 = 33
          > it is 33 not 33.33 , how to show the 33.33 %
          > Thanks[/color]

          "%.2f%%" % (100./3.)

          Not quite the most readable expression in python.. ;)

          George


          Comment

          • Terry Hancock

            #6
            Re: How to show percentage

            On Thursday 22 September 2005 12:51 pm, Sen-Lung Chen wrote:[color=blue]
            > Dear All:
            > I have a question of show percentage.
            > For example ,I want to show the percentage of 1/3 = 33.33%
            >
            > I use the 1*100/3 = 33
            > it is 33 not 33.33 , how to show the 33.33 %
            > Thanks[/color]

            In addition to needing a floating point value as other
            posters have noted, you're going to need a format string,
            e.g.:[color=blue][color=green][color=darkred]
            >>> print "%5.2f%%" % (100.0/3)[/color][/color][/color]
            33.33%

            Note the need to escape "%" as it is normally the format
            specifier character.

            Cheers,
            Terry

            --
            Terry Hancock ( hancock at anansispacework s.com )
            Anansi Spaceworks http://www.anansispaceworks.com

            Comment

            • Steve Holden

              #7
              Re: How to show percentage

              Sen-Lung Chen wrote:[color=blue]
              > Dear All:
              > I have a question of show percentage.
              > For example ,I want to show the percentage of 1/3 = 33.33%
              >
              > I use the 1*100/3 = 33
              > it is 33 not 33.33 , how to show the 33.33 %
              > Thanks
              >[/color]
              You should by now know enough answers. The easiest way to ensure that
              the result is floating point is to cast it as

              pct = 100.0 * v) / N

              This is guaranteed to work in all past and future Python versions
              without setting any options. Then you can format is using the % operator.

              regards
              Steve
              --
              Steve Holden +44 150 684 7255 +1 800 494 3119
              Holden Web LLC www.holdenweb.com
              PyCon TX 2006 www.pycon.org

              Comment

              • Steve Holden

                #8
                Re: How to show percentage

                Steve Holden wrote:[color=blue]
                > Sen-Lung Chen wrote:
                >[color=green]
                >>Dear All:
                >> I have a question of show percentage.
                >>For example ,I want to show the percentage of 1/3 = 33.33%
                >>
                >> I use the 1*100/3 = 33
                >>it is 33 not 33.33 , how to show the 33.33 %
                >> Thanks
                >>[/color]
                >
                > You should by now know enough answers. The easiest way to ensure that
                > the result is floating point is to cast it as
                >
                > pct = 100.0 * v) / N
                >
                > This is guaranteed to work in all past and future Python versions
                > without setting any options. Then you can format is using the % operator.
                >
                > regards
                > Steve[/color]

                .... apart from the obvious syntax error, that is. Sigh.

                pct = (100.0 * v) / N

                regards
                Steve
                --
                Steve Holden +44 150 684 7255 +1 800 494 3119
                Holden Web LLC www.holdenweb.com
                PyCon TX 2006 www.pycon.org

                Comment

                Working...