473,466 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 10038
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.