473,228 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,228 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 10021
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: tertius | last post by:
Hi, I'm trying to round my float total to the nearest .05 cents. 12.01 should produce 12.00 0.14 should produce 0.10 2.28 " 2.25 703.81 " 703.80 ...
1
by: Yachi | last post by:
Hi, i need to round: 3° decimal between 1 and 5 LOW 3° decimal between 6 and 9 UP Thanks
0
by: dregier | last post by:
attached is my full query that I am currently working on: Declare @counterday1 int Declare @counterday2 int set @counterday1 = '20' set @counterday2 = '629' Declare @CounterMin07_01 float...
5
by: Peter Scheurer | last post by:
Hi, we found some strange behavior when operating with floats and round(). The following simplified statement reproduces the problem. select 6.56 - round(convert(float, 6.56), 2) from...
6
by: Jef Driesen | last post by:
I need to implement a function to implement the rounding of floating point values. At the moment i have two different implementations, depending on the type of the return value (integer or double)....
6
by: ng_mr | last post by:
No, not a question about "banker's rounding" or whatever it's called. I want to round a double to the nearest 100th, so I perform the following: // original is a double double result =...
3
by: Terje Barman | last post by:
Hi! I have sumarized float values and I'd like to round the sum to a certain precision. Say I have 1.234 and want it rounded to 1.23. Which method can I use? Terje
7
by: ma740988 | last post by:
Consider the equation (flight dynamics stuff): Yaw (Degrees) = Azimuth Angle(Radians) * 180 (Degrees) / 3.1415926535897932384626433832795 (Radians) There's a valid reason to use single...
5
by: shapper | last post by:
Hello, I have the following: int a; int b; var c c = a / (float)b; I used float to not get an int value as result. I think in C# I must. But know I want to round c to something like 3.48...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...

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.