473,387 Members | 1,575 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,387 software developers and data experts.

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 6745
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
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.