This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mdealencar
Recipients mark.dickinson, mdealencar, skrah
Date 2014-02-04.20:46:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <CAFvk2pHcuttRdkkdiih33pT1HW7=CVr8=hERT6BDAVRW3eES2w@mail.gmail.com>
In-reply-to <20140204175433.GA20486@sleipnir.bytereef.org>
Content
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.
History
Date User Action Args
2014-02-04 20:46:44mdealencarsetrecipients: + mdealencar, mark.dickinson, skrah
2014-02-04 20:46:44mdealencarlinkissue20502 messages
2014-02-04 20:46:44mdealencarcreate