How to truncate/round-off decimal numbers?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Girish Sahani

    #1

    How to truncate/round-off decimal numbers?

    Hi,

    I want to truncate every number to 2 digits after the decimal point. I
    tried the following but it doesnt work.
    [color=blue][color=green][color=darkred]
    >>> a = 2
    >>> b = 3
    >>> round(a*1.0 / b,2)[/color][/color][/color]
    0.6700000000000 0004

    Inspite of specifying 2 in 2nd attribute of round, it outputs all the
    digits after decimal.
  • MTD

    #2
    Re: How to truncate/round-off decimal numbers?

    > >>> a = 2[color=blue][color=green][color=darkred]
    > >>> b = 3
    > >>> round(a*1.0 / b,2)[/color][/color]
    > 0.6700000000000 0004
    >
    > Inspite of specifying 2 in 2nd attribute of round, it outputs all the
    > digits after decimal.[/color]

    This is because of floating point inaccuracy. The system cannot
    accurately represent some integers, however it does its best to
    approximate them. Notice this:
    [color=blue][color=green][color=darkred]
    >>> x = 2.0/3
    >>> x[/color][/color][/color]
    0.6666666666666 6663[color=blue][color=green][color=darkred]
    >>> round(x,2)[/color][/color][/color]
    0.6700000000000 0004[color=blue][color=green][color=darkred]
    >>> s = str(round(x,2))
    >>> s[/color][/color][/color]
    '0.67'

    Comment

    • MTD

      #3
      Re: How to truncate/round-off decimal numbers?

      > The system cannot[color=blue]
      > accurately represent some integers,[/color]

      Er, I meant FLOATS. Doh.

      Anyway, just to underline the example:
      [color=blue][color=green][color=darkred]
      >>> x[/color][/color][/color]
      0.6666666666666 6663[color=blue][color=green][color=darkred]
      >>> s = str(round(x,2))
      >>> s[/color][/color][/color]
      '0.67'[color=blue][color=green][color=darkred]
      >>> f = float(s)
      >>> f[/color][/color][/color]
      0.6700000000000 0004[color=blue][color=green][color=darkred]
      >>> f == round(x,2)[/color][/color][/color]
      True

      Comment

      • Laurent Pointal

        #4
        Re: How to truncate/round-off decimal numbers?

        Girish Sahani a écrit :[color=blue]
        > Hi,
        >
        > I want to truncate every number to 2 digits after the decimal point. I
        > tried the following but it doesnt work.
        >[color=green][color=darkred]
        >>>> a = 2
        >>>> b = 3
        >>>> round(a*1.0 / b,2)[/color][/color]
        > 0.6700000000000 0004
        >
        > Inspite of specifying 2 in 2nd attribute of round, it outputs all the
        > digits after decimal.[/color]

        There are two operations:

        1) calculate of round(), which return a float number result, with the
        well known problem of floating point numbers represantation (see the FAQ).

        2) print that number, where default printing of last expression result
        in the cli interpreter displays up to the highest precision, print
        statement works differently:
        [color=blue][color=green][color=darkred]
        >>> a=2
        >>> b=3
        >>> c=round(a*1.0/b,2)
        >>> c[/color][/color][/color]
        0.6700000000000 0004[color=blue][color=green][color=darkred]
        >>> print c[/color][/color][/color]
        0.67[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        A+

        Laurent.

        Comment

        • Scott David Daniels

          #5
          Re: How to truncate/round-off decimal numbers?

          Sybren Stuvel wrote:[color=blue]
          > Girish Sahani enlightened us with:[color=green]
          >> I want to truncate every number to 2 digits after the decimal point....[color=darkred]
          >>>>> a = 2
          >>>>> b = 3
          >>>>> round(a*1.0 / b,2)[/color]
          >> 0.6700000000000 0004[/color]
          >
          > If you want to format it, use '%.2f' % (float(a)/b)[/color]

          Sybren has this right. If you follow everyone else's advice,
          you'll eventually discover:
          [color=blue][color=green][color=darkred]
          >>> print round(11024. / 5000.1, 2)[/color][/color][/color]
          only gives you "2.2", not "2.20" (which is what, I suspect, you want).

          --Scott David Daniels
          scott.daniels@a cm.org

          Comment

          • Dan Bishop

            #6
            Re: How to truncate/round-off decimal numbers?

            MTD wrote:[color=blue][color=green]
            > > The system cannot
            > > accurately represent some integers,[/color]
            >
            > Er, I meant FLOATS. Doh.[/color]

            You were also right the first time. But it only applies to integers
            with more than 53 bits.

            Comment

            • Aahz

              #7
              Re: How to truncate/round-off decimal numbers?

              In article <mailman.7238.1 150794927.27775 .python-list@python.org >,
              Girish Sahani <girish@cse.iit b.ac.in> wrote:[color=blue]
              >
              >I want to truncate every number to 2 digits after the decimal point. I
              >tried the following but it doesnt work.
              >[color=green][color=darkred]
              >>>> a = 2
              >>>> b = 3
              >>>> round(a*1.0 / b,2)[/color][/color]
              >0.670000000000 00004
              >
              >Inspite of specifying 2 in 2nd attribute of round, it outputs all the
              >digits after decimal.[/color]

              You should also consider switching to the decimal module if you care
              about getting correct results.
              --
              Aahz (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

              "I saw `cout' being shifted "Hello world" times to the left and stopped
              right there." --Steve Gonedes

              Comment

              • per9000

                #8
                Re: How to truncate/round-off decimal numbers?

                Hi,

                just a thought: if you *always* work with "floats" with two decimals,
                you are in fact working with integers, but you represent them as a
                floats - confusing for the internal representation.

                So why not work with int(float * 100) instead? This way you only have
                to take care of roundoffs etc when dividing.

                "int (+|-|*) int" = int
                "int / int" = int / int + int % int

                Integers are nice, me like integers.

                /per9000

                Comment

                • per9000

                  #9
                  Re: How to truncate/round-off decimal numbers?

                  oops, should be something like this:

                  "int / int" = "int / int, int % int"

                  /per9000

                  Comment

                  • jean-michel bain-cornu

                    #10
                    Re: How to truncate/round-off decimal numbers?

                    > just a thought: if you *always* work with "floats" with two decimals,[color=blue]
                    > you are in fact working with integers, but you represent them as a
                    > floats - confusing for the internal representation.
                    >
                    > So why not work with int(float * 100) instead? This way you only have
                    > to take care of roundoffs etc when dividing.[/color]
                    And why won't you work with decimal module ?

                    Comment

                    • Nick Maclaren

                      #11
                      Re: How to truncate/round-off decimal numbers?


                      In article <1150875897.866 447.312150@g10g 2000cwb.googleg roups.com>,
                      "per9000" <per9000@gmail. com> writes:
                      |>
                      |> just a thought: if you *always* work with "floats" with two decimals,
                      |> you are in fact working with integers, but you represent them as a
                      |> floats - confusing for the internal representation.

                      No, you aren't - you are working with fixed-point, which is something
                      that is neither integers nor floating-point, but is somewhere in
                      between. I am (just) old enough to remember when it was used for
                      numeric work, and to have used it for that myself, but not old enough
                      to have done any numeric work using fixed-point hardware.

                      |> So why not work with int(float * 100) instead? This way you only have
                      |> to take care of roundoffs etc when dividing.

                      And multiplying, and calling most mathematical functions.


                      Regards,
                      Nick Maclaren.

                      Comment

                      • per9000

                        #12
                        Re: How to truncate/round-off decimal numbers?

                        Nick Maclaren wrote:[color=blue]
                        > |> just a thought: if you *always* work with "floats" with two decimals,
                        > |> you are in fact working with integers, but you represent them as a
                        > |> floats - confusing for the internal representation.
                        >
                        > No, you aren't - you are working with fixed-point[/color]

                        Nick, your answer has so many layers, I'll try to explain how I think
                        :-D

                        1) if you use integers you can think of them as having one part bigger
                        than 100 and one part smaller than 100, like so:[color=blue][color=green][color=darkred]
                        >>> a = 11122
                        >>> (a/100,a%100)[/color][/color][/color]
                        (111, 22)
                        Here the output 111,22 looks like something else than an integer, but
                        this is just a matter of representation. a *is* an integer, but we
                        represent it *as if it was* a "decimal" number. (Compare with
                        (minutes,second s) or (euro,cents) or (feet,inch) or any other
                        "arbitrary" position system)

                        2) If we use floats with two decimals[color=blue][color=green][color=darkred]
                        >>> b = 222.33
                        >>> b[/color][/color][/color]
                        222.33000000000 001
                        they look like fix-point numbers (I had to look it up
                        http://en.wikipedia.org/wiki/Fixed-point :-D) but python stores it
                        (correct me if I am wrong) as a float (or double or quad or whatever).
                        If we want to work with fix-point aritmetics we have to invent new
                        functions to do most math.

                        3) Most "decimal numbers" cannot be stored exactly as floats - that is
                        why b gave the ugly print. But some can, f.x[color=blue][color=green][color=darkred]
                        >>> quart = 0.25
                        >>> quart[/color][/color][/color]
                        0.25
                        quart translates to a finite "decimal" number in binary (0.01 I think).

                        The point is: representing should-be integers as floats makes you loose
                        precision (negligable probalby but still...).

                        4)[color=blue]
                        > |> So why not work with int(float * 100) instead? This way you only have
                        > |> to take care of roundoffs etc when dividing.
                        >
                        > And multiplying, and calling most mathematical functions.[/color]
                        You are correct of course. My mistake.

                        But, the multiplication is exact before you start rounding off - I wont
                        start counting ordos for int*int vs. float*float, but it could have
                        some advantages[color=blue][color=green][color=darkred]
                        >>> a[/color][/color][/color]
                        11122[color=blue][color=green][color=darkred]
                        >>> b[/color][/color][/color]
                        22233[color=blue][color=green][color=darkred]
                        >>> a*b[/color][/color][/color]
                        247275426[color=blue][color=green][color=darkred]
                        >>> (a*b/10000,a*b%10000 )[/color][/color][/color]
                        (24727, 5426)
                        On the other hand you will quickly loose accuracy if you perform
                        multiple multiplications or divisions or use other mathematical
                        functions.

                        5) So, when could this way of thinking be useful? Well, rarely, but if
                        you only add/subtract "decimals" and/or multiply "decimals" with whole
                        numbers or if you want to use some non-metric system to do scientific
                        stuff (compute square feet when having (feet1,inch1) * (feet2,inch2)
                        assuming that inches are atomic.)

                        This could of course be extended to
                        (feet, inch, quarter_of_afoo t, sixteeth_of_a_f oot) if you'd like - it
                        is all a matter of representation.

                        Regards,
                        Per
                        ----
                        "It is a gift. A gift to the foes of 'the Terrorists'. Why not
                        use this 'terrorism'? Long has my father, 'George Bush Sr',
                        kept the forces of 'the terrorists' at bay. By the blood of
                        our people are your lands kept safe. Give 'the land of the
                        brave' the weapon of the enemy. Let us use it against him."

                        Comment

                        • Scott David Daniels

                          #13
                          Re: How to truncate/round-off decimal numbers?

                          Nick Maclaren wrote: (of fixed point)[color=blue]
                          > .... I am (just) old enough to remember when it was used for
                          > numeric work, and to have used it for that myself, but not old enough
                          > to have done any numeric work using fixed-point hardware.[/color]

                          You are using fixed point hardware today. Fixed point tracked the
                          "decimal point" (or "binary point" or whatever) in the mind of the
                          programmer, not in the state of the hardware. There never was
                          "fixed point hardware," it was simply a way of viewing the results
                          of the gates of the same hardware we use today.

                          --
                          --Scott David Daniels
                          scott.daniels@a cm.org

                          Comment

                          • Nick Maclaren

                            #14
                            Re: How to truncate/round-off decimal numbers?


                            In article <44997ea8$1@nnt p0.pdx.net>,
                            Scott David Daniels <scott.daniels@ acm.org> writes:
                            |> Nick Maclaren wrote: (of fixed point)
                            |> > .... I am (just) old enough to remember when it was used for
                            |> > numeric work, and to have used it for that myself, but not old enough
                            |> > to have done any numeric work using fixed-point hardware.
                            |>
                            |> You are using fixed point hardware today. Fixed point tracked the
                            |> "decimal point" (or "binary point" or whatever) in the mind of the
                            |> programmer, not in the state of the hardware. There never was
                            |> "fixed point hardware," it was simply a way of viewing the results
                            |> of the gates of the same hardware we use today.

                            Er, no. Analogue hardware never was fixed-point, and I have used that :-)

                            More relevantly, you are using a very parochial and unusual interpretation
                            of the word 'hardware'. In normal usage, it includes everything below
                            the instruct set specification (now often called the ABI). And I can
                            assure you that there is a considerable difference between the hardware
                            that provides a floating-point interface at the ABI and that which
                            provides a fixed-point interface.


                            Regards,
                            Nick Maclaren.

                            Comment

                            Working...