Everyone suggesting the use of the f
string format code is implicitly assuming that it's okay to fix the number of digits after the decimal point. That seems like a very shaky assumption to me. However, if you don't make that assumption, there is no built-in mechanism to do what you want. This is the best hack I came up with when faced with a similar problem (in a PDF generator -- numbers in PDF can't use exponential notation). You probably want to take all the b
s off the strings, and there may be other Python3-isms in here.
_ftod_r = re.compile(
br'^(-?)([0-9]*)(?:\.([0-9]*))?(?:[eE]([+-][0-9]+))?$')
def ftod(f):
"""Print a floating-point number in the format expected by PDF:
as short as possible, no exponential notation."""
s = bytes(str(f), 'ascii')
m = _ftod_r.match(s)
if not m:
raise RuntimeError("unexpected floating point number format: {!a}"
.format(s))
sign = m.group(1)
intpart = m.group(2)
fractpart = m.group(3)
exponent = m.group(4)
if ((intpart is None or intpart == b'') and
(fractpart is None or fractpart == b'')):
raise RuntimeError("unexpected floating point number format: {!a}"
.format(s))
# strip leading and trailing zeros
if intpart is None: intpart = b''
else: intpart = intpart.lstrip(b'0')
if fractpart is None: fractpart = b''
else: fractpart = fractpart.rstrip(b'0')
if intpart == b'' and fractpart == b'':
# zero or negative zero; negative zero is not useful in PDF
# we can ignore the exponent in this case
return b'0'
# convert exponent to a decimal point shift
elif exponent is not None:
exponent = int(exponent)
exponent += len(intpart)
digits = intpart + fractpart
if exponent <= 0:
return sign + b'.' + b'0'*(-exponent) + digits
elif exponent >= len(digits):
return sign + digits + b'0'*(exponent - len(digits))
else:
return sign + digits[:exponent] + b'.' + digits[exponent:]
# no exponent, just reassemble the number
elif fractpart == b'':
return sign + intpart # no need for trailing dot
else:
return sign + intpart + b'.' + fractpart