471,353 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,353 software developers and data experts.

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
Jul 18 '05 #1
4 9920
Please use a word wrap of 75 characters or less.

Jason Tesser wrote:
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


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

Jul 18 '05 #2
David C. Fox wrote:
Please use a word wrap of 75 characters or less.

Jason Tesser wrote:
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


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


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/

Jul 18 '05 #3
Andy Todd wrote:

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

Regards,
Andy


Thanks. I didn't know about that one.

David

Jul 18 '05 #4
what you did was fine -
a = 2.123
round(a,2) 2.1200000000000001


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" <JT*****@nbbc.edu> wrote in message
news:ma**********************************@python.o rg...
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

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by tertius | last post: by
1 post views Thread by Yachi | last post: by
6 posts views Thread by Jef Driesen | last post: by
6 posts views Thread by ng_mr | last post: by
3 posts views Thread by Terje Barman | last post: by
5 posts views Thread by shapper | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.