sigfig#

sigfig(x, sigfigs=4, SI=False, sep=False, keepints=False, formats=None)[source]#

Return a string representation of variable x with sigfigs number of significant figures

Note: sc.sigfig() and sc.sigfigs() are aliases.

Parameters:
  • x (int/float/list/arr) – the number(s) to round

  • sigfigs (int) – number of significant figures to round to (if None, use ‘g’ format)

  • SI (bool) – whether to use SI notation (only for numbers >1)

  • sep (bool/str) – if provided, use as thousands separator

  • keepints (bool) – never round ints

  • formats (str/list) – custom format suffixes; if str (e.g. ‘kmb’), split into chars; if list (e.g. [‘k’,’m’,’bn’]), use as-is for 1e3, 1e6, etc.

Examples:

x = 3432.3842
sc.sigfig(x, SI=True) # Returns '3.432K'
sc.sigfig(x, sep=True) # Returns '3,432'
sc.sigfig(x, SI=True, formats='kmb') # Returns '3.432k' (lowercase)
sc.sigfig(x, sigfigs=None) # Returns '3432.38' (uses 'g' format)

vals = np.random.rand(5)
sc.sigfig(vals, sigfigs=3)
New in version 3.0.0: changed default number of significant figures from 5 to 4; return list rather than tuple; changed SI suffixes to uppercase
New in version 3.2.6: “formats” argument; use ‘g’ format when sigfigs=None