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
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:
>
>
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
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)
>
> >
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(
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
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
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.
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
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?
-
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
>>
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
12 matches
Mail list logo