import mpmath
import math


def mpfmt(a, num_digits=6):
  """Formats a mpmath float to given number of decimals."""
  log10_a = mpmath.log(abs(a), 10)
  scaling = int(math.floor(log10_a))
  a0 = float(a * mpmath.mpf('10')**(-scaling))
  return "mpf('%se%d')" % (round(a0, num_digits), scaling)