1

I understand the value of exponents, but typically when displaying decimal values to an end user, it's easier for the layman to understand normal decimal values. When I perform the following, I'd rather the display value of the decimal be 50, instead of:

>>> Decimal('22679.6185') / Decimal('28.349523125') / 16
Decimal('5E+1')

Is this possible without quantizing or doing anything to modify the actual value? Also, why does it display a short value like this as an exponent and some longer values in their normal decimal form? Is this a product of division (irony intended)?

orokusaki
  • 55,146
  • 59
  • 179
  • 257

3 Answers3

2

You're seeing the default representation, which you can change by subclassing Decimal and overriding __str__ and/or __repr__.

Note that __repr__ is just implemented like return "Decimal('%s')" % str(self), but you should try and preserve the invariant that eval(repr(d)) == d.

Probably what you're more interested in is not modifying the default str output or the representation of the class, but just controlling the display format of an instance. In this case, you should just be able to use the str.format features, e.g.

>>> num = Decimal('5E+1')
>>> num
Decimal('5E+1')
>>> print("{:f}".format(num))
50
wim
  • 338,267
  • 99
  • 616
  • 750
1

See: Significant figures in the decimal module (which admittedly tells you to use .quantize()). The main problem is that you must keep track of the number of significant digits manually.

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775
  • I don't think anyone else understood the question I was asking, which was how to display the decimal normally (without an exponential representation of it), so I appreciate your answer, even though it isn't what I was hoping to hear :) – orokusaki Mar 14 '12 at 00:33
  • Ah ... in that case, you might want to start with `.quantize()`, then go through `.as_tuple()` and format it manually. This is kind of a pain in the butt but is what I do in my "money" module, which I plan to clean up someday and put on github or something. :-) (However I'm doing formatting with optional leading or trailing CR, DB, parentheses, and all the other accounting things, and I even handle Indian Rupee formatting with lakh and crore.) – torek Mar 14 '12 at 00:38
  • 1
    Also: if your python is new enough (2.7), the `'{:f}'.format()` method (@wim below) works great. If not ... (I'm stuck with python back to 2.5 if not earlier, so, yeah :-) ) – torek Mar 14 '12 at 00:54
0

Directly parse the Decimal to int or float you want.

int(decimal.Decimal('22679.6185') / decimal.Decimal('28.349523125') / 16)

or

float(decimal.Decimal('22679.6185') / decimal.Decimal('28.349523125') / 16)

22679.6185 / 28.349523125 is exactly equals 800. and it shows Decimal("8E2") without anything wrong.

the precision depends on the context object. check it by using "decimal.getcontext()".It looks like following:

Context(prec=28, rounding=ROUND_HALF_EVEN, ....)

the "prec" is what you want. Try this.

 decimal.Decimal("42.5") / decimal.Decimal("37.1")

It results in 28 significant figures.

orokusaki
  • 55,146
  • 59
  • 179
  • 257
lantaozi
  • 1
  • 1