Re: Rounding a number

2015-04-30 Thread Dave Angel
On 04/30/2015 06:35 PM, Seymore4Head wrote: On Thu, 30 Apr 2015 22:00:17 +0200, Thijs Engels wrote: round(65253, -3) might be what you are looking for... On Thu, Apr 30, 2015, at 21:49, Seymore4Head wrote: I have this page book marked. https://mkaz.com/2012/10/10/python-string-format/ I a

Re: Rounding a number

2015-04-30 Thread Seymore4Head
On Thu, 30 Apr 2015 22:00:17 +0200, Thijs Engels wrote: >round(65253, -3) > >might be what you are looking for... > > >On Thu, Apr 30, 2015, at 21:49, Seymore4Head wrote: >> I have this page book marked. >> https://mkaz.com/2012/10/10/python-string-format/ >> >> I am getting numbers from sixty t

Re: Rounding a number

2015-04-30 Thread Mark Lawrence
On 30/04/2015 21:00, Thijs Engels wrote: On Thu, Apr 30, 2015, at 21:49, Seymore4Head wrote: I have this page book marked. https://mkaz.com/2012/10/10/python-string-format/ I am getting numbers from sixty thousand to two hundred thousand. I would like to round them to the nearest thousand. So 6

Re: Rounding a number

2015-04-30 Thread Thijs Engels
round(65253, -3) might be what you are looking for... On Thu, Apr 30, 2015, at 21:49, Seymore4Head wrote: > I have this page book marked. > https://mkaz.com/2012/10/10/python-string-format/ > > I am getting numbers from sixty thousand to two hundred thousand. > I would like to round them to the

Re: Rounding off Values of dicts (in a list) to 2 decimal points

2013-10-03 Thread tripsvt
On Thursday, October 3, 2013 11:03:17 AM UTC-7, Neil Cerutti wrote: > On 2013-10-03, trip...@gmail.com wrote: > > > thekey=[{"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": > > > 100.0, "b": 0.0, "c": 0.0, "d": 0.0}, {"a": 80.0, "b": 0.0, > > > "c": 10.0, "d": 10.0}, {"a": 90.0, "b": 0.0, "c"

Re: Rounding off Values of dicts (in a list) to 2 decimal points

2013-10-03 Thread Neil Cerutti
On 2013-10-03, trip...@gmail.com wrote: > thekey=[{"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": > 100.0, "b": 0.0, "c": 0.0, "d": 0.0}, {"a": 80.0, "b": 0.0, > "c": 10.0, "d": 10.0}, {"a": 90.0, "b": 0.0, "c": 0.0, "d": > 10.0}] > > However, at the URL, the values show up as 90.4327869412

Re: Rounding off Values of dicts (in a list) to 2 decimal points

2013-10-03 Thread Peter Otten
trip...@gmail.com wrote: > On Wednesday, October 2, 2013 10:01:16 AM UTC-7, tri...@gmail.com wrote: >> am trying to round off values in a dict to 2 decimal points but have been >> unsuccessful so far. The input I have is like this: >> >> >> >> >> >> y = [{'a': 80.0, 'b': 0.0786235, 'c': 1

Re: Rounding off Values of dicts (in a list) to 2 decimal points

2013-10-03 Thread tripsvt
On Wednesday, October 2, 2013 10:01:16 AM UTC-7, tri...@gmail.com wrote: > am trying to round off values in a dict to 2 decimal points but have been > unsuccessful so far. The input I have is like this: > > > > > > y = [{'a': 80.0, 'b': 0.0786235, 'c': 10.0, 'd': 10.6742903}, {'a': > 80.

Re: Rounding off Values of dicts (in a list) to 2 decimal points

2013-10-02 Thread Neil Cerutti
On 2013-10-02, trip...@gmail.com wrote: > am trying to round off values in a dict to 2 decimal points > but have been unsuccessful so far. The input I have is like > this: > > y = [{'a': 80.0, 'b': 0.0786235, 'c': 10.0, 'd': 10.6742903}, {'a': > 80.73246, 'b': 0.0, 'c': 10.780323, 'd': 10.

Re: Rounding off Values of dicts (in a list) to 2 decimal points

2013-10-02 Thread Joel Goldstick
On Wed, Oct 2, 2013 at 1:01 PM, wrote: > am trying to round off values in a dict to 2 decimal points but have been > unsuccessful so far. The input I have is like this: > > > y = [{'a': 80.0, 'b': 0.0786235, 'c': 10.0, 'd': 10.6742903}, {'a': > 80.73246, 'b': 0.0, 'c': 10.780323, 'd': 10.0

Re: Rounding off Values of dicts (in a list) to 2 decimal points

2013-10-02 Thread Jussi Piitulainen
trip...@gmail.com writes: > am trying to round off values in a dict to 2 decimal points but > have been unsuccessful so far. The input I have is like this: > > y = [{'a': 80.0, 'b': 0.0786235, 'c': 10.0, 'd': 10.6742903}, > {'a': 80.73246, 'b': 0.0, 'c': 10.780323, 'd': 10.0}, {'a': >

Re: Rounding off Values of dicts (in a list) to 2 decimal points

2013-10-02 Thread Skip Montanaro
> def roundingVals_toTwoDeci(): > global y > for d in y: > for k, v in d.items(): > v = ceil(v*100)/100.0 > return > roundingVals_toTwoDeci() > > > > But it is not working - I am still getting the old values. You're not assigning the rou

Re: Rounding up to the next 100

2010-01-21 Thread casevh
On Jan 21, 1:58 pm, noydb wrote: > Sorry, although what I really need is the string-number rounded UP > every time.  So if the number is 3890.32, it needs to go to 3900; if > the number is 3811.345, it needs to go to 3900 also. > > So, Florian's answer works. Another option is using math.ceil and

Re: Rounding up to the next 100

2010-01-21 Thread Alf P. Steinbach
* noydb: Sorry, although what I really need is the string-number rounded UP every time. So if the number is 3890.32, it needs to go to 3900; if the number is 3811.345, it needs to go to 3900 also. So, Florian's answer works. You might also consider -100*(-3579.127893//100) :-) Which avoi

RE: Rounding up to the next 100

2010-01-21 Thread Michael . Coll-Barth
> From: Arnaud Delobelle > > > > >>> XstrNmbr = 3579.127893 > > I meant > >>> XstrNmbr = "3579.127893" > > > >>> round(float(XstrNmbr), -2) > > 3600.0 Ah, then you will need to cast it first. >>> XstrNmbr = '3579.127893' >>> round(float(XstrNmbr) ,-2) 3600.0 The information contained in

Re: Rounding up to the next 100

2010-01-21 Thread noydb
Sorry, although what I really need is the string-number rounded UP every time. So if the number is 3890.32, it needs to go to 3900; if the number is 3811.345, it needs to go to 3900 also. So, Florian's answer works. -- http://mail.python.org/mailman/listinfo/python-list

Re: Rounding up to the next 100

2010-01-21 Thread noydb
On Jan 21, 4:30 pm, michael.coll-ba...@verizonwireless.com wrote: > > From: noydb > > If one has a floating number as a string, is there a spiffy way to > > round that string-number UP to the nearest 100? > > > XstrNmbr = 3579.127893 -- would want to round that to 3600. > > What's wrong with round?

Re: Rounding up to the next 100

2010-01-21 Thread Alf P. Steinbach
* michael.coll-ba...@verizonwireless.com: From: noydb If one has a floating number as a string, is there a spiffy way to round that string-number UP to the nearest 100? XstrNmbr = 3579.127893 -- would want to round that to 3600. What's wrong with round? round( XstrNmbr, -2 ) seems to d

Re: Rounding up to the next 100

2010-01-21 Thread Florian Diesch
noydb writes: > If one has a floating number as a string, is there a spiffy way to > round that string-number UP to the nearest 100? > > XstrNmbr = 3579.127893 -- would want to round that to 3600. math.ceil(3579.127893/100)*100 Florian -- GUIs programmieren mit Python und Glade:

RE: Rounding up to the next 100

2010-01-21 Thread Michael . Coll-Barth
> From: noydb > If one has a floating number as a string, is there a spiffy way to > round that string-number UP to the nearest 100? > > XstrNmbr = 3579.127893 -- would want to round that to 3600. What's wrong with round? round( XstrNmbr, -2 ) seems to do the trick. Or do you want to get ri

Re: Rounding up to the next 100

2010-01-21 Thread Arnaud Delobelle
Arnaud Delobelle writes: > >>> XstrNmbr = 3579.127893 I meant >>> XstrNmbr = "3579.127893" > >>> round(float(XstrNmbr), -2) > 3600.0 -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

Re: Rounding up to the next 100

2010-01-21 Thread Arnaud Delobelle
noydb writes: > If one has a floating number as a string, is there a spiffy way to > round that string-number UP to the nearest 100? > > XstrNmbr = 3579.127893 -- would want to round that to 3600. > > Thanks for any help! >>> XstrNmbr = 3579.127893 >>> round(float(XstrNmbr), -2) 3600.0 >>> HTH

Re: Rounding to the nearest 5

2009-01-30 Thread David
Benjamin J. Racine wrote: Doesn't this work? round_by_5.py import sys def round_by_5(x= sys.argv[0]): x = x/5. x = round(x) x = x*5 print(x) return x Ben R. I am learning, I got this to work fine; #!/usr/bin/python import sys def round_by_5(x = sys.argv[1]): x =

RE: Rounding to the nearest 5

2009-01-30 Thread Benjamin J. Racine
-list-bounces+bjracine=glosten@python.org [mailto:python-list-bounces+bjracine=glosten@python.org] On Behalf Of D'Arcy J.M. Cain Sent: Friday, January 30, 2009 6:07 AM To: Steven D'Aprano Cc: python-list@python.org Subject: Re: Rounding to the nearest 5 On 30 Jan 2009 06:23:1

Re: Rounding to the nearest 5

2009-01-30 Thread D'Arcy J.M. Cain
On 30 Jan 2009 06:23:17 GMT Steven D'Aprano wrote: > On Fri, 30 Jan 2009 00:24:47 -0500, D'Arcy J.M. Cain wrote: > > That appears to be rounding to nearest 10, not 5. Clarify your > > requirements first. > > Look again. 36 => 35. You are correct. I should have ommitted my first sentence and em

Re: Rounding to the nearest 5

2009-01-30 Thread Gary Herron
todp...@hotmail.com wrote: > How can you make python round numbers to the nearest 5: > > Example: > > 3 => 0 > 8 => 10 > 23.2 => 20 > 36 => 35 > 51.5 => 50 round(n,-1) rounds to the nearest 10, so round(n*2,-1)/2 will round to the nearest five. Gary Herron > > > Thanks! > > -

Re: Rounding to the nearest 5

2009-01-30 Thread Tim Chase
Steven D'Aprano wrote: On Thu, 29 Jan 2009 18:26:34 -0600, Tim Chase wrote: How can you make python round numbers to the nearest 5: Example: 3 => 0 8 => 10 23.2 => 20 36 => 35 51.5 => 50 I'm not sure *any* rounding system will give those results. Round towards zero. 8 => 10 ? One ca

Re: Rounding to the nearest 5

2009-01-29 Thread Steven D'Aprano
On Fri, 30 Jan 2009 00:24:47 -0500, D'Arcy J.M. Cain wrote: > On Thu, 29 Jan 2009 16:06:09 -0800 > "todp...@hotmail.com" wrote: >> How can you make python round numbers to the nearest 5: >> >> Example: >> >> 3 => 0 >> 8 => 10 >> 23.2 => 20 >> 36 => 35 >> 51.5 => 50 > > That appears to be rou

Re: Rounding to the nearest 5

2009-01-29 Thread Miles
On Thu, Jan 29, 2009 at 7:26 PM, Tim Chase wrote: >> How can you make python round numbers to the nearest 5: >> Example: >> 3 => 0 >> 8 => 10 >> 23.2 => 20 >> 36 => 35 >> 51.5 => 50 > > I'm not sure *any* rounding system will give those results. def bogoround(n): n1 = n / 5.0 return int(

Re: Rounding to the nearest 5

2009-01-29 Thread D'Arcy J.M. Cain
On Thu, 29 Jan 2009 16:06:09 -0800 "todp...@hotmail.com" wrote: > How can you make python round numbers to the nearest 5: > > Example: > > 3 => 0 > 8 => 10 > 23.2 => 20 > 36 => 35 > 51.5 => 50 That appears to be rounding to nearest 10, not 5. Clarify your requirements first. -- D'Arcy J.M.

Re: Rounding to the nearest 5

2009-01-29 Thread Steven D'Aprano
On Thu, 29 Jan 2009 18:26:34 -0600, Tim Chase wrote: >> How can you make python round numbers to the nearest 5: >> >> Example: >> >> 3 => 0 >> 8 => 10 >> 23.2 => 20 >> 36 => 35 >> 51.5 => 50 > > I'm not sure *any* rounding system will give those results. Round towards zero. > 3 should roun

Re: Rounding to the nearest 5

2009-01-29 Thread MRAB
todp...@hotmail.com wrote: How can you make python round numbers to the nearest 5: Example: 3 => 0 8 => 10 23.2 => 20 36 => 35 51.5 => 50 Divide by 5, round the result, then multiply by 5. -- http://mail.python.org/mailman/listinfo/python-list

Re: Rounding to the nearest 5

2009-01-29 Thread Tim Chase
How can you make python round numbers to the nearest 5: Example: 3 => 0 8 => 10 23.2 => 20 36 => 35 51.5 => 50 I'm not sure *any* rounding system will give those results. 3 should round up to 5 (not down to 0) and 23.2 should round up to 25 (not down to 20) in the same way that 8 rounds

Re: Rounding a number to nearest even

2008-04-17 Thread Lie
On Apr 13, 7:20 pm, Steve Holden <[EMAIL PROTECTED]> wrote: > Lie wrote: > > On Apr 12, 3:44 am, hdante <[EMAIL PROTECTED]> wrote: > [snip] > > > In short, choosing that x.0 is rounded down and x.5 is rounded up is > > arbitrary but not without a reason. > > Don't "arbitrary" and "not without a rea

Re: Rounding a number to nearest even

2008-04-15 Thread Arnaud Delobelle
On 11 Apr, 21:29, Gabriel Genellina <[EMAIL PROTECTED]> wrote: > ... If the numbers to be rounded come from a > measurement, the left column is not just a number but the representant > of an interval (as Mikael said, the're quantized). 2.3 means that the > measurement was closer to 2.3 than to 2.2

Re: Rounding a number to nearest even

2008-04-15 Thread Chris
On Apr 15, 12:33 pm, Chris <[EMAIL PROTECTED]> wrote: > On Apr 15, 11:47 am, Duncan Booth <[EMAIL PROTECTED]> > wrote: > > > Chris <[EMAIL PROTECTED]> wrote: > > > even is closer to even.75 than even+1.25.  Why should it be rounded > > > up ? > > > Because the OP wants to round values to the neares

Re: Rounding a number to nearest even

2008-04-15 Thread Chris
On Apr 15, 11:47 am, Duncan Booth <[EMAIL PROTECTED]> wrote: > Chris <[EMAIL PROTECTED]> wrote: > > even is closer to even.75 than even+1.25.  Why should it be rounded > > up ? > > Because the OP wants to round values to the nearest integer. Only values of > the form 'x.5' which have two nearest va

Re: Rounding a number to nearest even

2008-04-15 Thread Duncan Booth
Chris <[EMAIL PROTECTED]> wrote: > even is closer to even.75 than even+1.25. Why should it be rounded > up ? Because the OP wants to round values to the nearest integer. Only values of the form 'x.5' which have two nearest values use 'nearest even' to disambiguate the result. See http://en.wi

Re: Rounding a number to nearest even

2008-04-15 Thread colas . francis
On 14 avr, 20:02, Thomas Dybdahl Ahle <[EMAIL PROTECTED]> wrote: > On Fri, 2008-04-11 at 03:14 -0700, bdsatish wrote: > > The built-in function round( ) will always "round up", that is 1.5 is > > rounded to 2.0 and 2.5 is rounded to 3.0. > > > If I want to round to the nearest even, that is > > > m

Re: Rounding a number to nearest even

2008-04-15 Thread Chris
On Apr 15, 11:22 am, Sjoerd Mullender <[EMAIL PROTECTED]> wrote: > Thomas Dybdahl Ahle wrote: > > On Fri, 2008-04-11 at 03:14 -0700, bdsatish wrote: > >> The built-in function round( ) will always "round up", that is 1.5 is > >> rounded to 2.0 and 2.5 is rounded to 3.0. > > >> If I want to round to

Re: Rounding a number to nearest even

2008-04-15 Thread Sjoerd Mullender
Thomas Dybdahl Ahle wrote: > On Fri, 2008-04-11 at 03:14 -0700, bdsatish wrote: >> The built-in function round( ) will always "round up", that is 1.5 is >> rounded to 2.0 and 2.5 is rounded to 3.0. >> >> If I want to round to the nearest even, that is >> >> my_round(1.5) = 2# As expected >>

Re: Rounding a number to nearest even

2008-04-15 Thread Thomas Dybdahl Ahle
On Fri, 2008-04-11 at 03:14 -0700, bdsatish wrote: > The built-in function round( ) will always "round up", that is 1.5 is > rounded to 2.0 and 2.5 is rounded to 3.0. > > If I want to round to the nearest even, that is > > my_round(1.5) = 2# As expected > my_round(2.5) = 2# Not 3,

Re: Rounding a number to nearest even

2008-04-14 Thread [EMAIL PROTECTED]
On Apr 11, 4:14 am, bdsatish <[EMAIL PROTECTED]> wrote: > I'm interested in rounding numbers of the form "x.5" depending upon > whether x is odd or even. Any idea about how to implement it ? Side note: A specialized use for this is in the US Field Artillery, where it's called "artillery expressio

Re: Rounding a number to nearest even

2008-04-13 Thread Mark Dickinson
On Apr 13, 4:18 am, Lie <[EMAIL PROTECTED]> wrote: [...] > it and there is nothing else in it, but in the second number range > (barely above 1 to 2) the number 1.0 is not included while the number > 2.0 is contained in it, clearly not a clean separation of numbers in > the form of y.x where y is p

Re: Rounding a number to nearest even

2008-04-13 Thread Steve Holden
Lie wrote: > On Apr 12, 3:44 am, hdante <[EMAIL PROTECTED]> wrote: [snip] > > In short, choosing that x.0 is rounded down and x.5 is rounded up is > arbitrary but not without a reason. Don't "arbitrary" and "not without a reason" directly contradict one another? regards Steve -- Steve Holden

Re: Rounding a number to nearest even

2008-04-13 Thread Lie
On Apr 12, 3:44 am, hdante <[EMAIL PROTECTED]> wrote: (snip) > > In this table, we consider that a number is rounded down when the > > But then, the "Round up" table gives inconsistent results if, by the > same argument, we consider 2.0 -> 2 rounding up. (you get 12 round ups > and 8 round downs j

Re: Rounding a number to nearest even

2008-04-11 Thread Arnaud Delobelle
On Apr 11, 8:27 pm, Mark Dickinson <[EMAIL PROTECTED]> wrote: > On Apr 11, 2:33 pm, Lie <[EMAIL PROTECTED]> wrote: [...] > > Another mistake, in an unquantized value the probability of getting > > exactly 0.5 (or any other number specified) is not 0 but an > > infinitesimal (i.e. lim(x) where x ->

Re: Rounding a number to nearest even

2008-04-11 Thread hdante
On Apr 11, 3:33 pm, Lie <[EMAIL PROTECTED]> wrote: > > That old-school rounding method you're taught is based on a wrong > assumption of the nature of number. In the past, rounding algorithm is > based on this: > > Original => (RoundUp(u|d|n), RoundNearestEven(u|d|n) > ... > 1.0 => 1(n), 1(n) > 1.1

Re: Rounding a number to nearest even

2008-04-11 Thread Gabriel Genellina
On 11 abr, 15:33, Lie <[EMAIL PROTECTED]> wrote: > On Apr 11, 10:19 pm, Mikael Olofsson <[EMAIL PROTECTED]> wrote: > > That's exactly how I was taught to do rounding in what-ever low-level > > class it was. The idea is to avoid a bias, which assumes that the > > original values are already quantiz

Re: Rounding a number to nearest even

2008-04-11 Thread Mark Dickinson
On Apr 11, 2:33 pm, Lie <[EMAIL PROTECTED]> wrote: > In this table, we consider that a number is rounded down when the > number is equal to truncated value (the number without fractional > part), while round up is equal to truncated value + 1 or truncated > value -1 if value is negative (Actually t

Re: Rounding a number to nearest even

2008-04-11 Thread Lie
On Apr 11, 10:19 pm, Mikael Olofsson <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] commented about rounding towards even numbers > from mid-way between integers as opposed to for instance always rounding > up in those cases: > > > Strange request though, why do you need it that way, because 2.5 is

Re: Rounding a number to nearest even

2008-04-11 Thread Mark Dickinson
On Apr 11, 10:29 am, hdante <[EMAIL PROTECTED]> wrote: > Strangely, a "faster" version is: > > def fast_round(x): > if x % 1 != 0.5: return round(x) > return 2.0*round(x/2.0) You should be a little bit careful with the test x%1 == 0.5 if x might be negative: >>> x = -0.5 + 2**-54 >>> x

Re: Rounding a number to nearest even

2008-04-11 Thread Robert Kern
[EMAIL PROTECTED] wrote: > Strange request though, why do you need it that way, because 2.5 is > CLOSER to 3 than to 2... Uhhh, no it isn't. (3 - 2.5) == (2.5 - 2) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad

Re: Rounding a number to nearest even

2008-04-11 Thread Mikael Olofsson
[EMAIL PROTECTED] commented about rounding towards even numbers from mid-way between integers as opposed to for instance always rounding up in those cases: > Strange request though, why do you need it that way, because 2.5 is > CLOSER to 3 than to 2... That's exactly how I was taught to do roundi

Re: Rounding a number to nearest even

2008-04-11 Thread Graham Breed
On Apr 11, 6:14 pm, bdsatish <[EMAIL PROTECTED]> wrote: > The built-in function round( ) will always "round up", that is 1.5 is > rounded to 2.0 and 2.5 is rounded to 3.0. > > If I want to round to the nearest even, that is > > my_round(1.5) = 2# As expected > my_round(2.5) = 2# Not

Re: Rounding a number to nearest even

2008-04-11 Thread hdante
On Apr 11, 11:13 am, Ivan Illarionov <[EMAIL PROTECTED]> wrote: > > Shorter version: > def round3k(x): > return x % 1 != 0.5 and round(x) or round(x / 2.) * 2. Strangely, a "faster" version is: def fast_round(x): if x % 1 != 0.5: return round(x) return 2.0*round(x/2.0) > > nums =

Re: Rounding a number to nearest even

2008-04-11 Thread Ivan Illarionov
On Apr 11, 5:49 pm, hdante <[EMAIL PROTECTED]> wrote: > On Apr 11, 9:45 am, bdsatish <[EMAIL PROTECTED]> wrote: > > > > > On Apr 11, 5:33 pm, bdsatish <[EMAIL PROTECTED]> wrote: > > > > HI Gerard, > > > > I think you've taken it to the best possible implementation. Thanks ! > > > On Apr 11, 5:14 pm

Re: Rounding a number to nearest even

2008-04-11 Thread hdante
On Apr 11, 9:45 am, bdsatish <[EMAIL PROTECTED]> wrote: > On Apr 11, 5:33 pm, bdsatish <[EMAIL PROTECTED]> wrote: > > > > > HI Gerard, > > > I think you've taken it to the best possible implementation. Thanks ! > > On Apr 11, 5:14 pm, Gerard Flanagan <[EMAIL PROTECTED]> wrote: > > > > In fact you c

Re: Rounding a number to nearest even

2008-04-11 Thread Ivan Illarionov
On Apr 11, 2:14 pm, bdsatish <[EMAIL PROTECTED]> wrote: > The built-in function round( ) will always "round up", that is 1.5 is > rounded to 2.0 and 2.5 is rounded to 3.0. > > If I want to round to the nearest even, that is > > my_round(1.5) = 2# As expected > my_round(2.5) = 2# Not

Re: Rounding a number to nearest even

2008-04-11 Thread bdsatish
On Apr 11, 5:33 pm, bdsatish <[EMAIL PROTECTED]> wrote: > HI Gerard, > > I think you've taken it to the best possible implementation. Thanks ! > On Apr 11, 5:14 pm, Gerard Flanagan <[EMAIL PROTECTED]> wrote: > > > In fact you can avoid the call to the builtin round: > > > --

Re: Rounding a number to nearest even

2008-04-11 Thread colas . francis
On 11 avr, 14:14, Gerard Flanagan <[EMAIL PROTECTED]> wrote: > On Apr 11, 2:05 pm, Gerard Flanagan <[EMAIL PROTECTED]> wrote: > > > On Apr 11, 12:14 pm, bdsatish <[EMAIL PROTECTED]> wrote: > > > > The built-in function round( ) will always "round up", that is 1.5 is > > > rounded to 2.0 and 2.5 is

Re: Rounding a number to nearest even

2008-04-11 Thread bdsatish
HI Gerard, I think you've taken it to the best possible implementation. Thanks ! On Apr 11, 5:14 pm, Gerard Flanagan <[EMAIL PROTECTED]> wrote: > In fact you can avoid the call to the builtin round: > > > def myround(x): > n = int(x) > if ab

Re: Rounding a number to nearest even

2008-04-11 Thread Gerard Flanagan
On Apr 11, 2:05 pm, Gerard Flanagan <[EMAIL PROTECTED]> wrote: > On Apr 11, 12:14 pm, bdsatish <[EMAIL PROTECTED]> wrote: > > > The built-in function round( ) will always "round up", that is 1.5 is > > rounded to 2.0 and 2.5 is rounded to 3.0. > > > If I want to round to the nearest even, that is >

Re: Rounding a number to nearest even

2008-04-11 Thread Gerard Flanagan
On Apr 11, 12:14 pm, bdsatish <[EMAIL PROTECTED]> wrote: > The built-in function round( ) will always "round up", that is 1.5 is > rounded to 2.0 and 2.5 is rounded to 3.0. > > If I want to round to the nearest even, that is > > my_round(1.5) = 2# As expected > my_round(2.5) = 2# No

Re: Rounding a number to nearest even

2008-04-11 Thread bdsatish
On Apr 11, 4:37 pm, Scott David Daniels <[EMAIL PROTECTED]> wrote: > bdsatish wrote: > > The built-in function round( ) will always "round up", that is 1.5 is > def rounded(v): > rounded = round(v) > if divmod(v, 1)[1] == .5 and divmod(rounded, 2)[1] == 1: > if v > 0: >

Re: Rounding a number to nearest even

2008-04-11 Thread bdsatish
On Apr 11, 4:24 pm, [EMAIL PROTECTED] wrote: > On Apr 11, 1:19 pm, [EMAIL PROTECTED] wrote: > > > couldn't you just do. > > > #untested > > new_round(n): > > answer = round(n) > > # is answer now odd > > if answer % 2: > > return answer - 1 > > else: > > return answer > > Whoops, th

Re: Rounding a number to nearest even

2008-04-11 Thread Scott David Daniels
bdsatish wrote: > The built-in function round( ) will always "round up", that is 1.5 is > rounded to 2.0 and 2.5 is rounded to 3.0. > > If I want to round to the nearest even, that is > > my_round(1.5) = 2# As expected > my_round(2.5) = 2# Not 3, which is an odd num > > I'm inter

Re: Rounding a number to nearest even

2008-04-11 Thread bdsatish
On Apr 11, 4:19 pm, [EMAIL PROTECTED] wrote: > couldn't you just do. > > #untested > new_round(n): > answer = round(n) > # is answer now odd > if answer % 2: > return answer - 1 > else: > return answer It fails for negative numbers: For -2.5 it gives -4.0 as answer whereas I expect

Re: Rounding a number to nearest even

2008-04-11 Thread cokofreedom
On Apr 11, 1:19 pm, [EMAIL PROTECTED] wrote: > couldn't you just do. > > #untested > new_round(n): > answer = round(n) > # is answer now odd > if answer % 2: > return answer - 1 > else: > return answer Whoops, this also affects odd numbers... Will try and find a GOOD solution late

Re: Rounding a number to nearest even

2008-04-11 Thread cokofreedom
couldn't you just do. #untested new_round(n): answer = round(n) # is answer now odd if answer % 2: return answer - 1 else: return answer -- http://mail.python.org/mailman/listinfo/python-list

Re: Rounding a number to nearest even

2008-04-11 Thread bdsatish
On Apr 11, 3:27 pm, [EMAIL PROTECTED] wrote: > On 11 avr, 12:14, bdsatish <[EMAIL PROTECTED]> wrote: > > > The built-in function round( ) will always "round up", that is 1.5 is > > rounded to 2.0 and 2.5 is rounded to 3.0. > > > If I want to round to the nearest even, that is > > > my_round(1.5) =

Re: Rounding a number to nearest even

2008-04-11 Thread colas . francis
On 11 avr, 12:14, bdsatish <[EMAIL PROTECTED]> wrote: > The built-in function round( ) will always "round up", that is 1.5 is > rounded to 2.0 and 2.5 is rounded to 3.0. > > If I want to round to the nearest even, that is > > my_round(1.5) = 2# As expected > my_round(2.5) = 2# Not 3

Re: ROUNDING???

2008-02-18 Thread Asun Friere
On Feb 19, 2:05 pm, 7stud <[EMAIL PROTECTED]> wrote: > An integer divided by an integer produces an integer. In computer > programming, that's called 'integer arithmetic', and any fractional > part of the result is chopped off(not rounded). In case you care, the "chopped off" bit is given by the

Re: ROUNDING???

2008-02-18 Thread subeen
You can use the round() function. And if you want to print, use %0.2f regards, Subeen. http://love-python.blogspot.com/ On Feb 19, 9:36 am, [EMAIL PROTECTED] (Aahz) wrote: > In article <[EMAIL PROTECTED]>, > katie smith <[EMAIL PROTECTED]> wrote: > > > > >in python im doing the problem 255/494

Re: ROUNDING???

2008-02-18 Thread Aahz
In article <[EMAIL PROTECTED]>, katie smith <[EMAIL PROTECTED]> wrote: > >in python im doing the problem 255/494 > >it keeps giving me 0 instead of .51 >what am i doing wrong? >>> from __future__ import division >>> 2/5 0.40002 In addition: >>> division _Feature((2, 2, 0, 'alpha

Re: ROUNDING???

2008-02-18 Thread Jeff Schwab
7stud wrote: > On Feb 18, 7:57 pm, katie smith <[EMAIL PROTECTED]> wrote: >> in python im doing the problem 255/494 >> >> it keeps giving me 0 instead of .51 >> what am i doing wrong? >> >> please help me I have been looking for hours >> >> >>

Re: ROUNDING???

2008-02-18 Thread Daniel Fetchinson
> in python im doing the problem 255/494 Try this: >>> 255.0/494.0 0.51619433198380571 >>> float(255)/float(494) 0.51619433198380571 > it keeps giving me 0 instead of .51 > what am i doing wrong? Nothing, integer division is not wrong :) > please help me I have been looking for hours

Re: ROUNDING???

2008-02-18 Thread 7stud
On Feb 18, 7:57 pm, katie smith <[EMAIL PROTECTED]> wrote: > in python im doing the problem 255/494 > > it keeps giving me 0 instead of .51 > what am i doing wrong? > > please help me I have been looking for hours > >       >

Re: Rounding

2007-12-18 Thread John Machin
On Dec 19, 7:21 am, "Ron Provost" <[EMAIL PROTECTED]> wrote: > Adding 0.5, then converting to an int always works for me. > > >>> x = 6.345 > >>> int( x + 0.5 ) > >>> 6 Always? >>> x = -6.345 >>> int(x + 0.5) -5 >>> I therefore deduce that you have never crossed the equator :-) Be careful, doin

Re: Rounding

2007-12-18 Thread Ron Provost
Adding 0.5, then converting to an int always works for me. >>> x = 6.345 >>> int( x + 0.5 ) >>> 6 Ron - Original Message - From: "katie smith" <[EMAIL PROTECTED]> To: Sent: Saturday, December 15, 2007 9:09 AM Subject: Rounding > if i have a number 6.345 and i wanted it to be 6 witho

Re: Rounding

2007-12-18 Thread Vladimir Rusinov
On 12/18/07, Lars Johansen <[EMAIL PROTECTED]> wrote: > > that does not round up.. > Yep, this is truncation. I'd read the question better. -- Vladimir Rusinov GreenMice Solutions: IT-решения на базе Linux http://greenmice.info/ -- http://mail.python.org/mailman/listinfo/python-list

Re: Rounding

2007-12-18 Thread Chris
On Dec 18, 10:53 am, "Vladimir Rusinov" <[EMAIL PROTECTED]> wrote: > On 12/15/07, katie smith <[EMAIL PROTECTED]> wrote: > > > > > if i have a number 6.345 and i wanted it to be 6 without subtracting .345 > > because it won't always be .345 what do i do? > > > how do i round to the nearest whole nu

Re: Rounding

2007-12-18 Thread John Machin
On Dec 18, 7:53 pm, "Vladimir Rusinov" <[EMAIL PROTECTED]> wrote: > On 12/15/07, katie smith <[EMAIL PROTECTED]> wrote: > > > > > if i have a number 6.345 and i wanted it to be 6 without subtracting .345 > > because it won't always be .345 what do i do? > > > how do i round to the nearest whole num

Re: Rounding

2007-12-18 Thread Lars Johansen
that does not round up.. int(round(6.345)) maybe tir, 18.12.2007 kl. 11.53 +0300, skrev Vladimir Rusinov: > > > On 12/15/07, katie smith <[EMAIL PROTECTED]> wrote: > if i have a number 6.345 and i wanted it to be 6 without > subtracting .345 because it won't always be .345 what do

Re: Rounding

2007-12-18 Thread Vladimir Rusinov
On 12/15/07, katie smith <[EMAIL PROTECTED]> wrote: > > if i have a number 6.345 and i wanted it to be 6 without subtracting .345 > because it won't always be .345 what do i do? > > how do i round to the nearest whole number. Or in this case round down. Is > there an easy way to round down to the n

Re: Rounding up to the nearest exact logarithmic decade

2006-02-28 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: > BTW, are the python-dev guys aware that 10 ** -1 = 0.10001 ? http://docs.python.org/tut/node16.html -- http://mail.python.org/mailman/listinfo/python-list

Re: Rounding up to the nearest exact logarithmic decade

2006-02-28 Thread Grant Edwards
On 2006-02-28, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > I like Fredrik's solution. If for some reason you are afraid of > logarithms, you could also do: > x = 4978 decades = [10 ** n for n in xrange(-1,8)] import itertools itertools.ifilter(lambda decade: x < decade, deca

Re: Rounding up to the nearest exact logarithmic decade

2006-02-28 Thread johnzenger
I like Fredrik's solution. If for some reason you are afraid of logarithms, you could also do: >>> x = 4978 >>> decades = [10 ** n for n in xrange(-1,8)] >>> import itertools >>> itertools.ifilter(lambda decade: x < decade, decades).next() 1 BTW, are the python-dev guys aware that 10 ** -1 =

Re: Rounding up to the nearest exact logarithmic decade

2006-02-28 Thread Grant Edwards
On 2006-02-28, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Quoting Derek Basch <[EMAIL PROTECTED]>: > >> Given a value (x) that is within the range (1e-1, 1e7) how do I round >> (x) up to the closest exact logarithmic decade? For instance: > > How about this: > > def roundup(x): > if x < 1:

Re: Rounding up to the nearest exact logarithmic decade

2006-02-28 Thread Derek Basch
Thanks effbot. I knew their had to be something buried in the math module that could help. ceil() it is! -- http://mail.python.org/mailman/listinfo/python-list

Re: Rounding up to the nearest exact logarithmic decade

2006-02-28 Thread Felipe Almeida Lessa
Em Ter, 2006-02-28 às 17:47 -0500, [EMAIL PROTECTED] escreveu: > Quoting Derek Basch <[EMAIL PROTECTED]>: > > > Given a value (x) that is within the range (1e-1, 1e7) how do I round > > (x) up to the closest exact logarithmic decade? For instance: > > How about this: > > def roundup(x): > if

Re: Rounding up to the nearest exact logarithmic decade

2006-02-28 Thread jao
Quoting Derek Basch <[EMAIL PROTECTED]>: > Given a value (x) that is within the range (1e-1, 1e7) how do I round > (x) up to the closest exact logarithmic decade? For instance: How about this: def roundup(x): if x < 1: return 1 else: return '1' + ('0' * len(str(int(x

Re: Rounding up to the nearest exact logarithmic decade

2006-02-28 Thread Fredrik Lundh
Derek Basch wrote: > Given a value (x) that is within the range (1e-1, 1e7) how do I round > (x) up to the closest exact logarithmic decade? For instance: > > 10**3 = 1000 > x = 4978 > 10**4 = 1 > x = 1 how about >>> import math >>> def roundup(x): ...return 10**math.ceil

Re: rounding problem

2005-02-25 Thread Nick Coghlan
Dan Bishop wrote: Your statement is misleading, because it suggests that your processor stores digits. It doesn't; it stores *bits*. And where does the word 'bit' come from, hmm? It couldn't possibly be an abbreviation of Binary digIT, could it? Cheers, Nick. -- Nick Coghlan | [EMAIL PROTECT

Re: rounding problem

2005-02-24 Thread Thomas Bartkus
"Dan Bishop" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > tom wrote: > > That last digit will *always* contain some arithmetic slop. > > Your statement is misleading, because it suggests that your processor > stores digits. It doesn't; it stores *bits*. Your explanation is much

Re: rounding problem

2005-02-23 Thread Dan Bishop
tom wrote: > On Wed, 23 Feb 2005 19:04:47 -0600, Andy Leszczynski wrote: > > > It is on Windows, Linux, Python 2.3: > > > > [GCC 3.3.2 (Mandrake Linux 10.0 3.3.2-6mdk)] on linux2 Type "help", > > "copyright", "credits" or "license" for more information. > > >>> a=1.1 > > >>> a > > 1.1

Re: rounding problem

2005-02-23 Thread tom
On Wed, 23 Feb 2005 19:04:47 -0600, Andy Leszczynski wrote: > It is on Windows, Linux, Python 2.3: > > [GCC 3.3.2 (Mandrake Linux 10.0 3.3.2-6mdk)] on linux2 Type "help", > "copyright", "credits" or "license" for more information. > >>> a=1.1 > >>> a > 1.1001 > >>> > >>> > >>> >

Re: rounding problem

2005-02-23 Thread Michael Hartl
> Is it normal? Yes. The interpreter prints back the repr of a, which reflects the imprecision inherent in floats. If you want '1.1', use the string returned by the str function. >>> a = 1.1 >>> a 1.1001 >>> repr(a) '1.1001' >>> str(a) '1.1' Michael -- Michael D. Hartl

Re: rounding problem

2005-02-23 Thread Kristian M Zoerhoff
In article <[EMAIL PROTECTED]>, Andy Leszczynski wrote: > >>> a=1.1 > >>> a > 1.1001 > >>> > > > Is it normal? Yes, for floating-point numbers. This is due to inherent imprecision in how floats are represented in hardware. If you can live with being a touch off that many decimal plac

Re: rounding problem

2005-02-23 Thread Aahz
In article <[EMAIL PROTECTED]>, Andy Leszczynski <[EMAIL PROTECTED]> wrote: >It is on Windows, Linux, Python 2.3: > >[GCC 3.3.2 (Mandrake Linux 10.0 3.3.2-6mdk)] on linux2 >Type "help", "copyright", "credits" or "license" for more information. > >>> a=1.1 > >>> a >1.1001 > >>> > > >Is

  1   2   >