Message210264
Thank you. This function accomplishes what I need, avoiding the
float->string->Decimal conversion path.
I will use a slight variation of it accepting floats and a precision value:
from decimal import Decimal, Contextdef sigdec(f, prec): x =
Context(prec=prec).create_decimal_from_float(f) adj = x.adjusted()
if adj >= prec - 1: return x else: return
x.quantize(Decimal(1).scaleb(adj-prec+1))
Since create_decimal_from_float() applies the precision upon conversion,
the +x trick is not needed. I also noticed that (adj >= prec - 1) does the
job, avoiding the else block in a few more cases. |
|
Date |
User |
Action |
Args |
2014-02-04 20:46:44 | mdealencar | set | recipients:
+ mdealencar, mark.dickinson, skrah |
2014-02-04 20:46:44 | mdealencar | link | issue20502 messages |
2014-02-04 20:46:44 | mdealencar | create | |
|