def to_units(val, spaces=0, dec_limit=2, postfix=''):
 decimals = 0
 if (val < 0):
  sign = '-'
 else:
  sign = ''
 val = str(abs(val)).strip()
 n = 0
 try:
  val = float(val)
 except:
  return ''
 while ((val > 1023.0) and (n < 5)):
  val = (val / 1024.0)
  n = (n + 1)
 unit = TAB_UNITS[n]
 if (not unit):
  unit = (' ' * spaces)
 if (n > dec_limit):
  decimals = 1
 else:
  decimals = 0
 fmt = ('%%s%%.%sf %%s%%s' % decimals)
 return (fmt % (sign, val, unit, postfix))

def to_units ( val , spaces = 0 , dec_limit = 2 , postfix = '' ) : decimals = 0 if ( val < 0 ) : sign = '-' else : sign = '' val = str ( abs ( val ) ) . strip ( ) n = 0 try : val = float ( val ) except : return '' while ( ( val > 1023.0 ) and ( n < 5 ) ) : val = ( val / 1024.0 ) n = ( n + 1 ) unit = TAB_UNITS [ n ] if ( not unit ) : unit = ( ' ' * spaces ) if ( n > dec_limit ) : decimals = 1 else : decimals = 0 fmt = ( '%%s%%.%sf %%s%%s' % decimals ) return ( fmt % ( sign , val , unit , postfix ) )
convert number to k/m/g/t/p notation add "spaces" if not ending in letter dig_limit==1 show single decimal for m and higher dig_limit==2 show single decimal for g and higher .