If I've got a Python Decimal, how can I reliably get the precise decimal string (ie, not scientific notation) representation of the number without trailing zeros?
For example, if I have:
>>> d = Decimal('1e-14')
I would like:
>>> get_decimal_string(d)
'0.00000000000001'
However:
- The
Decimalclass doesn't have anyto_decimal_stringmethod, or even anyto_radix_string(radix)(cf: https://docs.python.org/3/library/decimal.html#decimal.Context.to_eng_string) - The
%fformatter either defaults to rounding to 6 decimal places -'%f' %(d, ) ==> '0.000000'- or requires a precise number of decimal places. - The
{:f}.format(...)formatter appears to work -'{:f}'.format(d) ==> '0.00000000000001'- however I'm reluctant to trust that, as this actually runs counter to the documentation, which says "'f'… Displays the number as a fixed-point number. The default precision is 6" Decimal.__repr__andDecimal.__str__sometimes return scientific notation:repr(d) ==> "Decimal('1E-14')"
So, is there any way to get a decimal string from a Python Decimal? Or do I need to roll my own using Decimal.as_tuple()?