fork download
  1. from decimal import Decimal, Context, ROUND_DOWN
  2.  
  3. def format_adjusted_value(value):
  4. d = Decimal(value)
  5. exp = d.adjusted()
  6. return f'{d.normalize(Context(rounding=ROUND_DOWN, prec=1 + (exp > -2))):.{max(1 + (d.as_tuple().exponent < 0), -exp)}f}'
  7.  
  8. print(format_adjusted_value(0.00820289933))
  9. print(format_adjusted_value(0.00000025252))
  10. print(format_adjusted_value(0.858282815215))
  11. print(format_adjusted_value(0.075787842545))
  12. print(format_adjusted_value(1.100000010999))
  13. print(format_adjusted_value(0.0))
  14. print(format_adjusted_value(0.000000000000))
  15. print(format_adjusted_value(0.000365266565))
  16. print(format_adjusted_value(0.000036526656))
  17. print(format_adjusted_value(0.000003652665))
  18. print(format_adjusted_value(0.000000365266))
Success #stdin #stdout 0.04s 10472KB
stdin
Standard input is empty
stdout
0.008
0.0000002
0.85
0.07
1.10
0.0
0.0
0.0003
0.00003
0.000003
0.0000003