code: def gformat(val, length=11):
      """Format a number with '%g'-like format.
  
      Except that:
          a) the length of the output string will be of the requested length.
          b) positive numbers will have a leading blank.
          b) the precision will be as high as possible.
          c) trailing zeros will not be trimmed.
  
      The precision will typically be ``length-7``.
  
      Parameters
      ----------
      val : float
          Value to be formatted.
      length : int, optional
          Length of output string (default is 11).
  
      Returns
      -------
      str
          String of specified length.
  
      Notes
      ------
      Positive values will have leading blank.
  
      """
      if val is None or isinstance(val, bool):
          return f"{repr(val):>{length}s}"
      try:
          expon = int(log10(abs(val)))
      except (OverflowError, ValueError):
          expon = 0
      except TypeError:
          return f"{repr(val):>{length}s}"
  
      length = max(length, 7)
      form = "e"
      prec = length - 7
      if abs(expon) > 99:
          prec -= 1
      elif (expon > 0 and expon < (prec + 4)) or (
          expon <= 0 and -expon < (prec - 1)
      ):
          form = "f"
          prec += 4
          if expon > 0:
              prec -= expon
      return f"{val:{length}.{prec}{form}}"
  
file_path: lmfit-py/lmfit/printfuncs.py
imports: from math import log10
  import re
  import numpy
label_desc: Use image node_id 3 for calling a global function with example usage: gformat(val, length) and returns: val, repr, repr