Re: how to convert from Decimal('1.23456789') to Decimal('1.234')

2009-03-24 Thread Steven D'Aprano
On Mon, 23 Mar 2009 17:19:48 -0700, Mensanator wrote: >> >> And it also gives different results to my function: my function >> >> rounds to decimal places, yours to digits. Very >> >> different things. >> >> > Yeah, I know all about that. I work in Environmental Remediation. >> > That's real sci

Re: how to convert from Decimal('1.23456789') to Decimal('1.234')

2009-03-23 Thread Mensanator
On Mar 23, 5:42 pm, Steven D'Aprano wrote: > On Mon, 23 Mar 2009 10:06:23 -0700, Mensanator wrote: > > On Mar 23, 5:48 am, Steven D'Aprano > cybersource.com.au> wrote: > >> On Mon, 23 Mar 2009 01:45:53 -0700, Mensanator wrote: > >> >> but you can create a helper > >> >> function very easily: > >

Re: how to convert from Decimal('1.23456789') to Decimal('1.234')

2009-03-23 Thread Steven D'Aprano
On Mon, 23 Mar 2009 10:06:23 -0700, Mensanator wrote: > On Mar 23, 5:48 am, Steven D'Aprano cybersource.com.au> wrote: >> On Mon, 23 Mar 2009 01:45:53 -0700, Mensanator wrote: >> >> but you can create a helper >> >> function very easily: >> >> >> def round(dec, places, rounding=decimal.ROUND_HALF

Re: how to convert from Decimal('1.23456789') to Decimal('1.234')

2009-03-23 Thread Mensanator
On Mar 23, 5:48 am, Steven D'Aprano wrote: > On Mon, 23 Mar 2009 01:45:53 -0700, Mensanator wrote: > >> but you can create a helper > >> function very easily: > > >> def round(dec, places, rounding=decimal.ROUND_HALF_UP): return > >> dec.quantize(decimal.Decimal(str(10**-places)), rounding) > > >

Re: how to convert from Decimal('1.23456789') to Decimal('1.234')

2009-03-23 Thread Steven D'Aprano
On Mon, 23 Mar 2009 01:45:53 -0700, Mensanator wrote: >> but you can create a helper >> function very easily: >> >> def round(dec, places, rounding=decimal.ROUND_HALF_UP): � � return >> dec.quantize(decimal.Decimal(str(10**-places)), rounding) > > Still ugly. I would do this: > a = Decimal(

Re: how to convert from Decimal('1.23456789') to Decimal('1.234')

2009-03-23 Thread Mark Dickinson
On Mar 23, 6:40 am, valpa wrote: > I only need the 3 digits after '.' > > Is there any way other than converting from/to string? And in Python 3.0, just use the built-in round function: >>> from decimal import Decimal >>> round(Decimal('1.23456789'), 3) Decimal('1.235') This uses the rounding s

Re: how to convert from Decimal('1.23456789') to Decimal('1.234')

2009-03-23 Thread Mensanator
On Mar 23, 2:24�am, Steven D'Aprano wrote: > On Sun, 22 Mar 2009 23:40:38 -0700, valpa wrote: > > I only need the 3 digits after '.' > > > Is there any way other than converting from/to string? > > You should Read the Fine Manual: > > http://docs.python.org/library/decimal.html > > [quote] > The q

Re: how to convert from Decimal('1.23456789') to Decimal('1.234')

2009-03-23 Thread Mark Dickinson
On Mar 23, 7:01 am, alex23 wrote: > On Mar 23, 4:40 pm, valpa wrote: > > > I only need the 3 digits after '.' > > > Is there any way other than converting from/to string? > > I'm not sure if this is the canonical way but it works: > > >>> d = Decimal('1.23456789') > >>> three_places = Decimal('0.

Re: how to convert from Decimal('1.23456789') to Decimal('1.234')

2009-03-23 Thread Steven D'Aprano
On Sun, 22 Mar 2009 23:40:38 -0700, valpa wrote: > I only need the 3 digits after '.' > > Is there any way other than converting from/to string? You should Read the Fine Manual: http://docs.python.org/library/decimal.html [quote] The quantize() method rounds a number to a fixed exponent. This

Re: how to convert from Decimal('1.23456789') to Decimal('1.234')

2009-03-23 Thread Hyunchul Kim
In that case, I usually use # when rounding is proper, s = '1.23456789' print round(float(s)) or # when cut out is proper, from math import floor print floor(float(s)*1000)/1000 Hyunchul valpa wrote: I only need the 3 digits after '.' Is there any way other than converting from/to string? -

Re: how to convert from Decimal('1.23456789') to Decimal('1.234')

2009-03-23 Thread alex23
On Mar 23, 4:40 pm, valpa wrote: > I only need the 3 digits after '.' > > Is there any way other than converting from/to string? I'm not sure if this is the canonical way but it works: >>> d = Decimal('1.23456789') >>> three_places = Decimal('0.001') # or anything that has the exponent depth >>

how to convert from Decimal('1.23456789') to Decimal('1.234')

2009-03-22 Thread valpa
I only need the 3 digits after '.' Is there any way other than converting from/to string? -- http://mail.python.org/mailman/listinfo/python-list