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

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Tue Mar 24 03:11:19 EDT 2009


On Mon, 23 Mar 2009 17:19:48 -0700, Mensanator wrote:

>> >> And it also gives different results to my function: my function
>> >> rounds to <places> decimal places, yours to <i> digits. Very
>> >> different things.
>>
>> > 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.
>>
>> Okay, so in other words you solved your problem rather than the OP's
>> problem.
> 
> People other than the OP read these threads. Do you want to give people
> the impression that quantize is the only option?

The only option for what? Solving the OP's problem, or your problem, or 
some other problem? How many different problems are we going to solve in 
this thread?

At the very least, when taking a detour to solve a different problem, say 
so, and point out why your problem is a more interesting/useful problem 
to solve.


>> > Do
>> > you know what kind of hoops I have to jump through to get Access or
>> > Excel to round properly when doing unit conversion?
>>
>> I feel your pain.
> 
> So surely you don't want to give the impression that all Decimal is good
> for is emulating Excel?

That's a non sequitor. It does not follow that just because I sympathize 
with your difficulties with Excel, that I therefore want to give the 
impression that Decimal is only good for emulating Excel.


>> > Surely you're not so maive that you think dividing by 1000 simply
>> > moves the decimal point three places?
>>
>> Of course it does, if you're using real numbers.
> 
> Computers have real numbers?

You don't have to use computers to divide by 1000. As difficult as it is 
to believe, throughout history people have done mathematical calculations 
without the use of computing devices.


>> If you're using floats,
>> no, not quite, there are rounding issues involved, and underflow.
> 
> And that's why we have Decimal, right?

I'm sure that the Decimal standard wasn't invented just so people can 
divide by 1000.



-- 
Steven



More information about the Python-list mailing list