round/float question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason Tesser

    round/float question

    I am using Rekall which uses Python as it's scripting language and I have a question about Python. I am developing a POS system and am working on
    the checkout form. I pass a parameter named SaleID and another one named Total. I am trying to take total and convert it to a float with 2 decimals top be used as money obviously. What would be the proper syntax for this? I tried Total = round(Total [, 2]) i also tried that with float.

    Jason Tesser
    Web/Multimedia Programmer
    Northland Ministries Inc.
    (715)324-6900 x3050


  • David C. Fox

    #2
    Re: round/float question

    Please use a word wrap of 75 characters or less.

    Jason Tesser wrote:
    [color=blue]
    > I am using Rekall which uses Python as it's scripting language and I
    > have a question about Python. I am developing a POS system and am
    > working on the checkout form. I pass a parameter named SaleID and
    > another one named Total. I am trying to take total and convert it to
    > a float with 2 decimals top be used as money obviously. What would
    > be the proper syntax for this? I tried Total = round(Total [, 2]) i
    > also tried that with float.
    >
    > Jason Tesser Web/Multimedia Programmer Northland Ministries Inc.
    > (715)324-6900 x3050
    >
    >[/color]

    For monetary values, you are much better off avoiding floating point
    altogether. For example, you could express total in cents. Then, to
    convert to dollars and cents, you can use divmod(total, 100), which
    divides total by 100, returning the integer portion (the dollar amount)
    followed by the remainder (cents amount).

    For example:

    total = 599 # ($5.99)
    print "item $%d.%d" % divmod(total, 100)

    total = total + 275
    dollars, cents = divmod(total, 100)
    print "subtotal $%d.%d" % divmod(total, 100)

    taxrate = .05
    tax = int(round(total *taxrate))
    print "tax $%d.%d" % divmod(tax, 100)

    total = total + tax
    print "total $%d.%d" % divmod(total, 100)

    David

    Comment

    • Andy Todd

      #3
      Re: round/float question

      David C. Fox wrote:[color=blue]
      > Please use a word wrap of 75 characters or less.
      >
      > Jason Tesser wrote:
      >[color=green]
      >> I am using Rekall which uses Python as it's scripting language and I
      >> have a question about Python. I am developing a POS system and am
      >> working on the checkout form. I pass a parameter named SaleID and
      >> another one named Total. I am trying to take total and convert it to
      >> a float with 2 decimals top be used as money obviously. What would
      >> be the proper syntax for this? I tried Total = round(Total [, 2]) i
      >> also tried that with float.
      >>
      >> Jason Tesser Web/Multimedia Programmer Northland Ministries Inc.
      >> (715)324-6900 x3050
      >>
      >>[/color]
      >
      > For monetary values, you are much better off avoiding floating point
      > altogether. For example, you could express total in cents. Then, to
      > convert to dollars and cents, you can use divmod(total, 100), which
      > divides total by 100, returning the integer portion (the dollar amount)
      > followed by the remainder (cents amount).
      >
      > For example:
      >
      > total = 599 # ($5.99)
      > print "item $%d.%d" % divmod(total, 100)
      >
      > total = total + 275
      > dollars, cents = divmod(total, 100)
      > print "subtotal $%d.%d" % divmod(total, 100)
      >
      > taxrate = .05
      > tax = int(round(total *taxrate))
      > print "tax $%d.%d" % divmod(tax, 100)
      >
      > total = total + tax
      > print "total $%d.%d" % divmod(total, 100)
      >
      > David
      >[/color]

      Or you could use the fixed point module to represent all of your
      monetary items (http://fixedpoint.sourceforge.net/).

      Regards,
      Andy
      --
      --------------------------------------------------------------------------------
      From the desk of Andrew J Todd esq - http://www.halfcooked.com/



      Comment

      • David C. Fox

        #4
        Re: round/float question

        Andy Todd wrote:
        [color=blue]
        >
        > Or you could use the fixed point module to represent all of your
        > monetary items (http://fixedpoint.sourceforge.net/).
        >
        > Regards,
        > Andy[/color]

        Thanks. I didn't know about that one.

        David

        Comment

        • Sagiv Malihi

          #5
          Re: round/float question

          what you did was fine -[color=blue][color=green][color=darkred]
          >>> a = 2.123
          >>> round(a,2)[/color][/color][/color]
          2.1200000000000 001[color=blue][color=green][color=darkred]
          >>>[/color][/color][/color]

          round (var, ndigits) will return the variable rounded to as many digits
          after the decimal point as you want.
          (perhaps your problem was with the square brackets? they are obviously not
          needed )
          sagiv.


          "Jason Tesser" <JTesser@nbbc.e du> wrote in message
          news:mailman.10 65809636.26201. python-list@python.org ...
          I am using Rekall which uses Python as it's scripting language and I have a
          question about Python. I am developing a POS system and am working on
          the checkout form. I pass a parameter named SaleID and another one named
          Total. I am trying to take total and convert it to a float with 2 decimals
          top be used as money obviously. What would be the proper syntax for this?
          I tried Total = round(Total [, 2]) i also tried that with float.

          Jason Tesser
          Web/Multimedia Programmer
          Northland Ministries Inc.
          (715)324-6900 x3050



          Comment

          Working...