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 =
C
Mauricio de Alencar added the comment:
> You need to stop lecturing.
I'm sorry, I didn't mean to offend anyone. I just felt I was failing to
communicate the issue when I got the suggestion to use format(Decimal(1),
".2f").
> The above sentence you wrote directly cont
Mauricio de Alencar added the comment:
"Digits after the decimal mark" is not the same as "significant digits".
See https://en.wikipedia.org/wiki/Significant_figures
If I have a list of numbers [256.2, 1.3, 0.5] that have 3 significant digits
each, I would like to have them
Mauricio de Alencar added the comment:
String formatting is completely unaware of the concept of *significant digits*.
The only format that can get it right for every case is the 'e'. But then you
always get the exponent, which is undesirable. I was hopeful that the decimal
mo
Mauricio de Alencar added the comment:
I propose then to create a context setting that switches between the current
textual representation of a Decimal and one that is "right zeros padded" up to
context precision.
Such that the line:
print(Context(prec=4, precise
Mauricio de Alencar added the comment:
According to the docs (http://docs.python.org/3/library/decimal.html):
"The decimal module incorporates a notion of significant places so that 1.30 +
1.20 is 2.50. The trailing zero is kept to indicate significance. This is the
customary presentatio
New submission from Mauricio de Alencar:
The following code demonstrates an inconsistency of this method in dealing with
zeros after the decimal mark.
from decimal import Context
context = Context(prec=2)
for x in [100., 10., 1., 0.1]:
print(context.create_decimal_from_float(x