Re: [Tutor] rounding up to the nearest multiple of 8

2012-10-05 Thread Albert-Jan Roskam
> This is all fun, but what about the context?  Your original function > took an integer, not a string, and thus wasn't charged with measuring > string length, possibly multiple times.  Even so, each of these tests is > taking around a microsecond.  So are you expecting to do anything with > t

Re: [Tutor] rounding up to the nearest multiple of 8

2012-10-05 Thread Dave Angel
On 10/05/2012 07:23 AM, Albert-Jan Roskam wrote: > - Original Message - > >> From: Asokan Pichai >> To: tutor@python.org >> Cc: >> Sent: Friday, October 5, 2012 11:06 AM >> Subject: Re: [Tutor] rounding up to the nearest multiple of 8 >> >>

Re: [Tutor] rounding up to the nearest multiple of 8

2012-10-05 Thread Albert-Jan Roskam
- Original Message - > From: Asokan Pichai > To: tutor@python.org > Cc: > Sent: Friday, October 5, 2012 11:06 AM > Subject: Re: [Tutor] rounding up to the nearest multiple of 8 > > If you are doing so many times, it may be worth precomputing the padding for a >

Re: [Tutor] rounding up to the nearest multiple of 8

2012-10-05 Thread eryksun
On Fri, Oct 5, 2012 at 4:24 AM, Albert-Jan Roskam wrote: > > import timeit > ver1 = timeit.timeit(""" > import math > value = "1234" > value = "%-*s" % (int(math.ceil(len(value)/8.0)*8), value) > """) > ver2 = timeit.timeit(""" > value = "1234" > value = value.ljust( len(value) + (-len(value) % 8)

Re: [Tutor] rounding up to the nearest multiple of 8

2012-10-05 Thread Asokan Pichai
If you are doing so many times, it may be worth precomputing the padding for a given length and adding it by a look up on the length. For example: SPACE = ' ' MAX = 1000 TAB = 8 paddding = [ SPACE * (n % TAB) for n in range(MAX) ] . s = padding[len(s)] + s . -

Re: [Tutor] rounding up to the nearest multiple of 8

2012-10-05 Thread eryksun
On Fri, Oct 5, 2012 at 2:54 AM, Steven D'Aprano wrote: > > py> from __future__ import division > py> from math import ceil > py> "%*s" % (int(ceil(len(mystring)/8)*8), mystring) > '123412341234' > > > Or left-justified: > > py> "%-*s" % (int(ceil(len(mystring)/8)*8), mystring) > '123412341234

Re: [Tutor] rounding up to the nearest multiple of 8

2012-10-05 Thread Albert-Jan Roskam
- Original Message - > From: Steven D'Aprano > To: tutor@python.org > Cc: > Sent: Friday, October 5, 2012 8:54 AM > Subject: Re: [Tutor] rounding up to the nearest multiple of 8 > > On Thu, Oct 04, 2012 at 05:26:13PM -0400, eryksun wrote: >> On T

Re: [Tutor] rounding up to the nearest multiple of 8

2012-10-04 Thread Steven D'Aprano
On Thu, Oct 04, 2012 at 05:26:13PM -0400, eryksun wrote: > On Thu, Oct 4, 2012 at 4:04 PM, Joel Goldstick > wrote: > > > my_string = "123" > pad = 8 - len(my_string) % 8 > my_string = my_string + " " * pad > my_string > > '123 ' > > If len(my_string) is already a multiple

Re: [Tutor] rounding up to the nearest multiple of 8

2012-10-04 Thread eryksun
On Thu, Oct 4, 2012 at 4:04 PM, Joel Goldstick wrote: > my_string = "123" pad = 8 - len(my_string) % 8 my_string = my_string + " " * pad my_string > '123 ' If len(my_string) is already a multiple of 8, the above sets pad to 8: >>> s = "12345678" >>> pad = 8 - len(

Re: [Tutor] rounding up to the nearest multiple of 8

2012-10-04 Thread Joel Goldstick
On Thu, Oct 4, 2012 at 3:47 PM, Albert-Jan Roskam wrote: > Hi, > > The function below works, but it's such a kludge! Is there a way to make this > more elegant? > As said in the docstring, I want to round up a given integer to the nearest > multiple of 8. I was thinking > of something like math.

[Tutor] rounding up to the nearest multiple of 8

2012-10-04 Thread Albert-Jan Roskam
Hi,   The function below works, but it's such a kludge! Is there a way to make this more elegant? As said in the docstring, I want to round up a given integer to the nearest multiple of 8. I was thinking of something like math.ceil.   def _getPadding(charlen):     """ Helper function to replace n

Re: [Tutor] Rounding Error

2012-01-28 Thread Alan Gauld
On 28/01/12 05:55, Michael Lewis wrote: I am trying to round a float to two decimals, but I am getting the following error: Traceback (most recent call last): File "", line 1, in PaintingProject() File "C:/Python27/Homework/Labs/Lab 03_5.py", line 42, in PaintingProject print 'T

Re: [Tutor] Rounding Error

2012-01-27 Thread Devin Jeanpierre
On Sat, Jan 28, 2012 at 1:20 AM, Steven D'Aprano wrote: > And finally, we come all the way back to the beginning again and say That's > not the right way to do it! Don't round the number *outside* of the string > formatting, get the string formatting to do it for you: Reason being because repr(ro

Re: [Tutor] Rounding Error

2012-01-27 Thread Steven D'Aprano
Michael Lewis wrote: I am trying to round a float to two decimals, but I am getting the following error: Traceback (most recent call last): File "", line 1, in PaintingProject() File "C:/Python27/Homework/Labs/Lab 03_5.py", line 42, in PaintingProject print 'That will cost you $%f.'

Re: [Tutor] Rounding Error

2012-01-27 Thread Michael Lewis
Update I am trying to round a float to two decimals. > > Basically, I am writing a program to ask a user how many square feet they > need to paint. I then calculate how many cans of paint they need and will > also show the total purchase price. I've tried this two ways, and both ways > I am semi-s

[Tutor] Rounding Error

2012-01-27 Thread Michael Lewis
I am trying to round a float to two decimals, but I am getting the following error: Traceback (most recent call last): File "", line 1, in PaintingProject() File "C:/Python27/Homework/Labs/Lab 03_5.py", line 42, in PaintingProject print 'That will cost you $%f.' %(round(5.6523),2) Typ

Re: [Tutor] Rounding a Python float to the nearest half integer

2010-10-11 Thread Sithembewena Lloyd Dube
Thanks Shantanoo, finally got the hang of this. 2010/10/11 शंतनू (Shantanoo) > === > round(...) > round(number[, ndigits]) -> floating point number > > Round a number to a given precision in decimal digits (default 0 > digits). > This always returns a floating point number. Precisio

Re: [Tutor] Rounding a Python float to the nearest half integer

2010-10-11 Thread Shantanoo
=== round(...) round(number[, ndigits]) -> floating point number Round a number to a given precision in decimal digits (default 0 digits). This always returns a floating point number. Precision may be negative. === >>> round(1.23423,2) 1.23 >>> round(1.23623,2) 1.24 HTH. On Mon, O

Re: [Tutor] Rounding a Python float to the nearest half integer

2010-10-11 Thread Sithembewena Lloyd Dube
Thanks everyone. On Fri, Oct 8, 2010 at 11:44 PM, Wayne Werner wrote: > On Fri, Oct 8, 2010 at 7:58 AM, Sithembewena Lloyd Dube > wrote: > >> I realise that one cannot have a half integer :) I meant how would one >> round off to the first decimal nearest to either 0.5, or a whole number. >> >> U

Re: [Tutor] Rounding a Python float to the nearest half integer

2010-10-08 Thread Wayne Werner
On Fri, Oct 8, 2010 at 7:58 AM, Sithembewena Lloyd Dube wrote: > I realise that one cannot have a half integer :) I meant how would one > round off to the first decimal nearest to either 0.5, or a whole number. > > Ugh...does anyone get what I'm trying to articulate? :) sample input/output cases

Re: [Tutor] Rounding a Python float to the nearest half integer

2010-10-08 Thread Evert Rol
> @Evert, I didn't figure out that your response was a solution, thought it was > a question. Must be coffee time :P > > I tried it and, for instance, the rounded value (9) / 2 gave me 4.0 Couldn't > get it until I noticed that @Joel divided the roudned figure by a decimal > 2.0. That gave 4.5,

Re: [Tutor] Rounding a Python float to the nearest half integer

2010-10-08 Thread Sithembewena Lloyd Dube
@Evert, I didn't figure out that your response was a solution, thought it was a question. Must be coffee time :P I tried it and, for instance, the rounded value (9) / 2 gave me 4.0 Couldn't get it until I noticed that @Joel divided the roudned figure by a decimal 2.0. That gave 4.5, which is what

Re: [Tutor] Rounding a Python float to the nearest half integer

2010-10-08 Thread Sithembewena Lloyd Dube
Thanks everyone, I need to round to the nearest half (finally occured). Made some chnages to Wayne's code as follows: x = 4.4348 if x % 1 >= 0.5: round(x) # gives 5.0 if the the value of the expression x % 1 exceeds 0.5 else: x = round(x) + 0.5 # gives 4.5, as in this case. Many thanks!

Re: [Tutor] Rounding a Python float to the nearest half integer

2010-10-08 Thread Joel Goldstick
On Fri, Oct 8, 2010 at 9:00 AM, Evert Rol wrote: > > I realise that one cannot have a half integer :) I meant how would one > round off to the first decimal nearest to either 0.5, or a whole number. > > > > Ugh...does anyone get what I'm trying to articulate? :) > > Multiply by 2, round(), divide

Re: [Tutor] Rounding a Python float to the nearest half integer

2010-10-08 Thread Evert Rol
> I realise that one cannot have a half integer :) I meant how would one round > off to the first decimal nearest to either 0.5, or a whole number. > > Ugh...does anyone get what I'm trying to articulate? :) Multiply by 2, round(), divide by 2? > > On Fri, Oct 8, 2010 at 2:51 PM, Sithembewena

Re: [Tutor] Rounding a Python float to the nearest half integer

2010-10-08 Thread Wayne Werner
On Fri, Oct 8, 2010 at 7:51 AM, Sithembewena Lloyd Dube wrote: > Hi folks, > > Supposing I had the float 4.4348 and I wished to round it off to the > nearest half-integer upwards or downwards, how would I go about it? > You can use round: round(4.4348) -> 4.0 But if you want to specify the beha

Re: [Tutor] Rounding a Python float to the nearest half integer

2010-10-08 Thread Sithembewena Lloyd Dube
I realise that one cannot have a half integer :) I meant how would one round off to the first decimal nearest to either 0.5, or a whole number. Ugh...does anyone get what I'm trying to articulate? :) On Fri, Oct 8, 2010 at 2:51 PM, Sithembewena Lloyd Dube wrote: > Hi folks, > > Supposing I had t

[Tutor] Rounding a Python float to the nearest half integer

2010-10-08 Thread Sithembewena Lloyd Dube
Hi folks, Supposing I had the float 4.4348 and I wished to round it off to the nearest half-integer upwards or downwards, how would I go about it? Many thanks... -- Regards, Sithembewena Lloyd Dube http://www.lloyddube.com ___ Tutor maillist - Tutor

Re: [Tutor] Rounding to n significant digits

2009-09-02 Thread Sander Sweers
2009/9/2 Alan Gauld : > That having been said the two approaches, string or math, are equally > valid. I suspect the math version will be much slower since it calls > several functions but I haven't timed  it. > >>> def round_to_n(x, n): >>>         fmt = "%%.%de" % (n) >>>         return float( fm

Re: [Tutor] Rounding to n significant digits

2009-09-02 Thread Alan Gauld
"Richard Wagner" wrote I'm fairly new to Python and am trying to find a simple way to round floats to a specific number of significant digits. I found an old post on this list with exactly the same problem: The usual question is why would you want to lose precision in your data? Significan

Re: [Tutor] Rounding to n significant digits

2009-09-01 Thread Skipper Seabold
On Tue, Sep 1, 2009 at 10:00 PM, Skipper Seabold wrote: > On Tue, Sep 1, 2009 at 9:44 PM, Richard Wagner wrote: >> I'm fairly new to Python and am trying to find a simple way to round floats >> to a specific number of significant digits.  I found an old post on this >> list with exactly the same pr

Re: [Tutor] Rounding to n significant digits

2009-09-01 Thread Skipper Seabold
On Tue, Sep 1, 2009 at 9:44 PM, Richard Wagner wrote: > I'm fairly new to Python and am trying to find a simple way to round floats > to a specific number of significant digits.  I found an old post on this > list with exactly the same problem: > Python 2.5.4 (r254:67916, Apr 4 2009, 17:56:17) [G

[Tutor] Rounding to n significant digits

2009-09-01 Thread Richard Wagner
I'm fairly new to Python and am trying to find a simple way to round floats to a specific number of significant digits. I found an old post on this list with exactly the same problem: Is there something (a function?) in Python 2.

Re: [Tutor] rounding to the nearest 0.5

2007-10-25 Thread Eric Brunson
Bryan Fodness wrote: > > Is there a built-in function that will round to the nearest 0.5? Not that I know of, but "round( 2 * n ) / 2" will give you what you're looking for. > > > _

[Tutor] rounding to the nearest 0.5

2007-10-25 Thread Bryan Fodness
Is there a built-in function that will round to the nearest 0.5? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Rounding to nearest cent

2007-05-22 Thread Brian van den Broek
Jessica Brink said unto the world upon 05/22/2007 09:08 AM: > How would I round to the nearest cent when doing calculations with dollar > amounts? > > -Jess > Hi Jess, The decimal module was introduced in large part to facilitate financial calculations. Have a look at

[Tutor] Rounding to nearest cent

2007-05-22 Thread Jessica Brink
How would I round to the nearest cent when doing calculations with dollar amounts? -Jess ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Rounding number

2007-03-18 Thread Luke Paireepinart
Per Jr. Greisen wrote: > Hi, > > I use the method from math in order to round off the number: > > import math > > a = 1.3 > > b = round(a,3) > > which equals gives > b=1.4 > > is it possible to have the rounding such that > b = 1.400 Trailing zeroes aren't significant values so the computer won

[Tutor] Rounding number

2007-03-18 Thread Per Jr. Greisen
Hi, I use the method from math in order to round off the number: import math a = 1.3 b = round(a,3) which equals gives b=1.4 is it possible to have the rounding such that b = 1.400 so it keeps in my case the correct digits? Any help appreciate. Thanks in advance -- Best regards Per

Re: [Tutor] Rounding off

2007-03-18 Thread Kent Johnson
Dick Moores wrote: >> >>> print round(2.1345, 3) >> 2.135 > > But now I have a question. > > >>> print "%.3f" % 22.1345 > 22.134 > > Why the 22.134's? Because neither 2.1345 nor 22.1345 can be represented exactly in floating point, and the errors are in the opposite direction: In [4]: repr

Re: [Tutor] Rounding off

2007-03-18 Thread Dick Moores
At 04:59 AM 3/18/2007, Dick Moores wrote: >At 03:08 AM 3/18/2007, Per Jr. Greisen wrote: > >Hi, > > > >Is there an easy way - a module or method for which you can round > >off number such as > > > >2.1345 to 2.135 > >Well, there are > > >>> print "%.3f" % 2.1345 >2.135 > >or > > >>> print round(2

Re: [Tutor] Rounding off

2007-03-18 Thread Kent Johnson
Dick Moores wrote: > At 03:08 AM 3/18/2007, Per Jr. Greisen wrote: >> Hi, >> >> Is there an easy way - a module or method for which you can round >> off number such as >> >> 2.1345 to 2.135 > > Well, there are > > >>> print "%.3f" % 2.1345 > 2.135 > > or > > >>> print round(2.1345, 3) > 2.13

Re: [Tutor] Rounding off

2007-03-18 Thread Dick Moores
At 03:08 AM 3/18/2007, Per Jr. Greisen wrote: >Hi, > >Is there an easy way - a module or method for which you can round >off number such as > >2.1345 to 2.135 Well, there are >>> print "%.3f" % 2.1345 2.135 or >>> print round(2.1345, 3) 2.135 Dick Moores __

[Tutor] Rounding off

2007-03-18 Thread Per Jr. Greisen
Hi, Is there an easy way - a module or method for which you can round off number such as 2.1345 to 2.135 Any help or advice appreciated? -- Best regards Per Jr. Greisen ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/t

Re: [Tutor] Rounding a float to n significant digits

2006-12-04 Thread Dick Moores
At 12:52 PM 11/30/2006, Dick Moores wrote: >At 11:19 PM 11/27/2006, Dick Moores wrote: > >I just dug this Tim Smith creation out of the Tutor archive. > > > >def round_to_n(x, n): > > """ > > Rounds float x to n significant digits, in scientific notation. > > Written by Tim

Re: [Tutor] Rounding a float to n significant digits

2006-12-01 Thread Dick Moores
At 09:20 PM 11/30/2006, Tim Peters wrote: >[Dick Moores] >>... >>But isn't there a PRECISE answer to my question? > >Of course ;-) > >>Or is it OT? > >Well, it's really more a question about your machine's floating-point >hardware than about Python. Good explanations of exact limits for >IEEE-754

Re: [Tutor] Rounding a float to n significant digits

2006-11-30 Thread Tim Peters
[Dick Moores] > ... > But isn't there a PRECISE answer to my question? Of course ;-) > Or is it OT? Well, it's really more a question about your machine's floating-point hardware than about Python. Good explanations of exact limits for IEEE-754 floating-point hardware can be found many places o

Re: [Tutor] Rounding a float to n significant digits

2006-11-30 Thread Dick Moores
At 02:57 PM 11/30/2006, wesley chun wrote: >i think on a 32-bit platform, C doubles (IEEE754) are limited to 10 ** >308.25 which is pretty close to 2 ** 1024 > >-wesley Yes, that's close. Did some pinching down: == >>> 2**1023.94 1.797693134862174

Re: [Tutor] Rounding a float to n significant digits

2006-11-30 Thread wesley chun
i think on a 32-bit platform, C doubles (IEEE754) are limited to 10 ** 308.25 which is pretty close to 2 ** 1024 -wesley ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Rounding a float to n significant digits

2006-11-30 Thread Dick Moores
At 11:19 PM 11/27/2006, Dick Moores wrote: >I just dug this Tim Smith creation out of the Tutor archive. > >def round_to_n(x, n): > """ > Rounds float x to n significant digits, in scientific notation. > Written by Tim Peters. See his Tutor list post of 7/3/04 at > h

[Tutor] Rounding a float to n significant digits

2006-11-27 Thread Dick Moores
I just dug this Tim Smith creation out of the Tutor archive. def round_to_n(x, n): """ Rounds float x to n significant digits, in scientific notation. Written by Tim Peters. See his Tutor list post of 7/3/04 at http://mail.python.org/pipermail/tutor/2004-July/030324

Re: [Tutor] rounding

2005-01-30 Thread Kim Branson
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 ahhh, the second argument to round. That will do what i want. It was fairly late and i didn't see that argument in the docs. although now i look at it in the light of day its there. neat. I've replaced by divide by 10 stuff with that. now i have di

Re: [Tutor] rounding

2005-01-30 Thread Alan Gauld
> The round function will do what you want though not quite what you say you want :-) > > >>> round(15., -1) > 20.0 Interesting, I didn't remember the second parameter to round... let alone the fact it could be negative!! Batteries included once again! Alan G. ___

Re: [Tutor] rounding

2005-01-30 Thread Alan Gauld
> histograms for. I'd like to round the data to the nearest 10, i.e if > the value is 15.34 should we round down to 10? and conversely rounding > say 19.30 to 20. I'm thinking 15.5 and above would round up. Can anyone > point me a at a quick and painless way of achieving this? divide by 10, round

Re: [Tutor] rounding

2005-01-30 Thread Sean Perry
Kim Branson wrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Hi all, heres a quick one for you, I have a series of data that I am using dictionaries to build histograms for. I'd like to round the data to the nearest 10, i.e if the value is 15.34 should we round down to 10? and conversely rou

Re: [Tutor] rounding

2005-01-30 Thread Kent Johnson
The round function will do what you want though not quite what you say you want :-) >>> round(15., -1) 20.0 >>> round(15.5, -1) 20.0 >>> round(14, -1) 10.0 15.34 will round to 20, not 10, which is the closest multiple of 10. Kent Kim Branson wrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1

[Tutor] rounding

2005-01-30 Thread Kim Branson
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Hi all, heres a quick one for you, I have a series of data that I am using dictionaries to build histograms for. I'd like to round the data to the nearest 10, i.e if the value is 15.34 should we round down to 10? and conversely rounding say 19.30 to