0

I'm working with Python Decimal types, up to 8 decimal places, and for very small numbers, by default, they are displayed using exponential notation. I would prefer them to be displayed in non exponential notation. Example follows:

>>> from decimal import Decimal
>>> d1 = Decimal('0.00000100')
>>> d1
Decimal('0.00000100')
>>> str(d1)
0.00000100

In this case, d1 does just what I'd like. But if I use another with a slightly smaller number, I get exponent notation:

>>> d2 = Decimal('0.00000001')
>>> d2
Decimal('1E-8')
>>> str(d2)
'1E-8'

I realise that I can format this using format(d2, 'f') but there are lots of places where these values are passed directly to the front end of my app using an API so I'd rather not if possible.

I'm curious to know if this is something I can change?

Thanks!

Ludo
  • 2,739
  • 2
  • 28
  • 42
  • possible duplicate of [How can I format a decimal to always show 2 decimal places](http://stackoverflow.com/questions/1995615/how-can-i-format-a-decimal-to-always-show-2-decimal-places) – jww Sep 16 '14 at 11:52
  • 2
    If you have to pass it to your app via an API then why do you care about its formatting? Whether you pass 0.001 or 1e-3 *it's still the same number*. Have your app format it however you want. – Ffisegydd Sep 16 '14 at 11:53

1 Answers1

0

No, you cannot. If you really need to do this, you have following 2 options:

  1. If you keep your Decimals with two digits of precision, then converting them to strings with str will work fine.
  2. As suggested in the question, use format()

Source: Python Decimal Documentation

Rishi Dua
  • 2,296
  • 2
  • 24
  • 35