473,222 Members | 1,934 Online

# round() to nearest .05 ?

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

"%.02f"%100.0099 produces 100.01 (which I know is right)
No combination of round and/or "%.02f" works for me.

What's the best way to get there? Should I write a function to manage my
rounding or is there a simpler/better way?

TIA
T
Jul 18 '05 #1
4 6716
tertius wrote:
Hi,

I'm trying to round my float total to the nearest .05 cents.
If you want the *nearest* .05, this works:
def fivecents(f): .... return round(f*2.0, 1) / 2.0
.... for f in [ 12.01, 0.14, 2.28, 703.81]: .... print f, fivecents(f)
....
12.01 12.0
0.14 0.15
2.28 2.3
703.81 703.8
12.01 should produce 12.00
0.14 should produce 0.10
2.28 " 2.25
703.81 " 703.80
Your example seems to be rounding down always, not what I would call
nearest. Here is one that always rounds toward zero: def lowerfivecents(f): .... return int(f*20) / 20.0
.... for f in [ 12.01, 0.14, 2.28, 703.81]:

.... print f, lowerfivecents(f)
....
12.01 12.0
0.14 0.1
2.28 2.25
703.81 703.8

If you want to always round toward more negative, use math.floor()
instead of int().

Kent
"%.02f"%100.0099 produces 100.01 (which I know is right)
No combination of round and/or "%.02f" works for me.

What's the best way to get there? Should I write a function to manage my
rounding or is there a simpler/better way?

TIA
T

Jul 18 '05 #2
On Mon, 15 Nov 2004 22:52:42 +0200, tertius <te***@mighty.co.za> wrote:
Hi,

I'm trying to round my float total to the nearest .05 cents. Nearest which way? IWT 2.28 is nearer 2.30 than 2.25 ;-)
ISTM you are rounding _down_ to the nearest nickel. So you want to
drop fractional nickels. Which int will do if you calculate the number
of nickels. Then you can divide by 20. to get dollars again
12.01 should produce 12.00
0.14 should produce 0.10
2.28 " 2.25
703.81 " 703.80

"%.02f"%100.0099 produces 100.01 (which I know is right)
No combination of round and/or "%.02f" works for me.

What's the best way to get there? Should I write a function to manage my
rounding or is there a simpler/better way?

One way, just to play with subclassing float for constrained initial value:
values = [12.01, 0.14, 2.28, 703.81]
class Nickels(float): ... def __new__(cls, val):
... return float.__new__(cls, int(val*20.0)/20.)
... def __repr__(self): return '<Nickels %.2f>'%float(self)
... [Nickels(v) for v in values] [<Nickels 12.00>, <Nickels 0.10>, <Nickels 2.25>, <Nickels 703.80>] for value in values: print '%8.2f => %8.2f'%(value,Nickels(value)) ...
12.01 => 12.00
0.14 => 0.10
2.28 => 2.25
703.81 => 703.80

but it's probably silly compared to
def roundnickel(dollars): return int(dollars*20.0)/20.0 ... for value in values: print '%8.2f => %8.2f'%(value, roundnickel(value)) ...
12.01 => 12.00
0.14 => 0.10
2.28 => 2.25
703.81 => 703.80

What do you want to do for negative numbers? int "rounds" towards zero.
for value in -.01,.01,-.09,.09: print '%8.2f => %8.2f'%(value, roundnickel(value)) ...
-0.01 => 0.00
0.01 => 0.00
-0.09 => -0.05
0.09 => 0.05

BTW, if you wanted to use the class with all float operators, you'd have
to define the methods one way or another (there's some sneaky ways, but I think
the class is just a side trip ;-)
Nickels(.09) <Nickels 0.05> Nickels(.09) + .01 0.060000000000000005

Being a subclass of float, python figured out how to coerce Nickels
to float for the addition. If we add an adding method, we get
Nickels.__add__ = lambda self,other: Nickels(float(self)+ float(other))
Nickels(.09) + .01 <Nickels 0.05> Nickels(.09) + .06

<Nickels 0.10>

too much work ;-)

Regards,
Bengt Richter
Jul 18 '05 #3
> What's the best way to get there? Should I write a function to manage my
rounding or is there a simpler/better way?

Others have pointed out how to achieve the rounding - I just want to add
that using floats for currency values isn't the best choice. Depending on
your version of python, check out these links:

http://fixedpoint.sourceforge.net/
http://www.python.org/peps/pep-0327.html
--
Regards,

Diez B. Roggisch
Jul 18 '05 #4
tertius wrote:
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

"%.02f"%100.0099 produces 100.01 (which I know is right)
No combination of round and/or "%.02f" works for me.

What's the best way to get there? Should I write a function to manage my
rounding or is there a simpler/better way?

TIA
T

Thanks for all the help!
I meant nearest towards zero.

chrs
T
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 5 by: Marc A. Lefebvre US-775 | last post by: I have a table that I need to extract values, determine their ratio, and then round to the nearest 50. SELECT NAME, MFG, ROUND((WEIGHT/VOLUME),50) AS WV_RATIO, COUNT(*) OVER (ORDER BY NAME, MFG,... 6 by: Penguin | last post by: At some long ago time Steve Jorgensen answered thus: Subject: Re: How can I round a time? Newsgroups: comp.databases.ms-access Date: 1998/12/11 Access represents a date internally as a double... 9 by: Ronald W. Roberts | last post by: I'm having a problem understanding the Round function. Below are quotes from two books on VB.NET. The first book shows examples with one argument and how it rounds. The second book something... 3 by: Matt | last post by: Anybody noticed that SQL Server rounds up if the value is half way between two rounded values, but C#'s Decimal.Round(Decimal,Int32) rounds to nearest even number? >From MSDN: "When d is exactly... 0 by: thaisummitneel | last post by: sir I have created a database in ms-access.Connected database using ADODB I have written a query to round integer to nearest value such as con.execute"update tablename set... 4 by: naren2345 | last post by: Would this expression round an integer n to the nearest power of 4 ? ((n-1)|3) + 1 4 by: Jassim Rahma | last post by: I have a number, for example 0.152 or 1.729 and I want to allow to round to the first 0.010 or 0.050 or 0.100 6 by: dkirkdrei | last post by: I am looking for a way to round a number (which will be displayed as a dollar amount) to the nearest nickel. Using the php round function only allows you to control the number of decimal places in... 0 by: Edwin.Madari | last post by: >>round(76.1, -2) 100.0 80.0 76.0 builtin function round, will work for you...... Help on built-in function round in module __builtin__: round(...) round(number) -floating point number 1 by: isladogs | last post by: The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike... 0 by: veera ravala | last post by: ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow... 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... 3 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In... 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... 2 by: jimatqsi | last post by: The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art... 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... 1 by: davi5007 | last post by: Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.