Mauricio de Alencar added the comment: 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. ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue20502> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com