2

I'm looking for a method which takes a decimal.Decimal object x and returns a string s, such that:

  • x == Decimal(s)
  • re.match("^-?(0|[1-9][0-9]*)(.[0-9]*[1-9])?$", s) is not None
  • s != '-0'

In other words, the method doesn't change the value of the Decimal object; it returns a string representation which is never in scientific notation (e.g. 1e80), and never has any trailing zeros.

I would assume there is a standard library function which lets me do that, but I haven't found any. Do you know of any?

Jonas Kölker
  • 7,680
  • 3
  • 44
  • 51

1 Answers1

1

n.normalize() truncates decimals with more than 28 digits of precision, but you can use string formatting and a manual check for negative zero:

'{:f}'.format(abs(n) if n.is_zero() else n)
Blender
  • 289,723
  • 53
  • 439
  • 496