V
valpa
I only need the 3 digits after '.'
Is there any way other than converting from/to string?
Is there any way other than converting from/to string?
I only need the 3 digits after '.'
Is there any way other than converting from/to string?
I only need the 3 digits after '.'
Is there any way other than converting from/to string?
The quantize() method rounds a number to a fixed exponent. This method is
useful for monetary applications that often round results to a fixed
number of places:
Decimal('8')
[end quote]
In my opinion, that's hideously ugly, 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)
I'm not sure if this is the canonical way but it works:
Decimal('1.234')
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
The quantize() method rounds a number to a fixed exponent. This method is
useful for monetary applications that often round results to a fixed
number of places:
Decimal('8')
[end quote]
In my opinion, that's hideously ugly,
In looking at this for the first time, it struck
me as funny why the first argument to quantize
even requires a number since you can pass a
Conext as an argument. But if you do, precision
is ignored. Quantize isn't the way to do that.
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:
print Context.create_decimal(Context(i,ROUND_DOWN),a)
1
1.2
1.23
1.234
1.2345
I only need the 3 digits after '.'
Is there any way other than converting from/to string?
Still ugly. I would do this:
print Context.create_decimal(Context(i,ROUND_DOWN),a)
Well, that's hardly any less ugly.
And it also gives different results to my function: my function rounds to
<places> decimal places, yours to <i> digits. Very different things.
I wouldn't say so since there are no strings attached. Didn't the OP
specifically ask for a solution that didn't involve strings?
Yeah, I know all about that. I work in Environmental Remediation. That's
real science, where rounding to decimal places is strictly forbidden,
significant digits must be preserved. That means rounding to digits.
Do
you know what kind of hoops I have to jump through to get Access or
Excel to round properly when doing unit conversion?
Surely you're not so maive that you think dividing by 1000 simply moves
the decimal point three places?
No, the OP asked for a solution that didn't involve converting the
decimal number to a string first.
Besides, I don't believe you can specify the rounding mode unless you use
strings *wink*
Okay, so in other words you solved your problem rather than the OP's
problem.
I feel your pain.
Of course it does, if you're using real numbers.
If you're using floats,
no, not quite, there are rounding issues involved, and underflow.
I'm not sure why you raise this. Is this a general rant, or do you have a
specific criticism?
People other than the OP read these threads. Do you want to give people
the impression that quantize is the only option?
So surely you don't want to give the impression that all Decimal is good
for is emulating Excel?
Computers have real numbers?
And that's why we have Decimal, right?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.