#! /usr/bin/python



__author__ = 'Predrag Pejovic'
__license__ = 'GPLv3+'
__date__ = '2021-04-08'



################################################################################



from pylab import *



# prefixes, from https://en.wikipedia.org/wiki/Engineering_notation
# also, see https://en.wikipedia.org/wiki/Metric_prefix
#
prefs = {24:'Y', 21:'Z', 18:'E', 15:'P', 12:'T', 9:'G', 6:'M', 3:'k', 0:' ', 
-3:'m', -6:'u', -9:'n', -12:'p', -15:'f', -18:'a', -21:'z', -24:'y'}



# convert to exponential string, default format -xxx.xxe+yy
#
def toexpstring(x, fmtm = '7.2f'):

    y = abs(x)
    iee = floor(log10(y) // 3) * 3

    f = x / 10**iee
    estr = (r'{:' + fmtm + r'}').format(f)
    
    if iee >= 0:
        estr += 'e+'
    else:
        estr += 'e-'
        
    estr += '{:02d}'.format(abs(int(iee)))
    
    return estr



# convert to exponential string, default format -xxx.xx p
#
def toengstring(x, fmtm = '7.2f'):

    y = abs(x)
    iee = floor(log10(y)) // 3 * 3

    f = x / 10**iee
    estr = (r'{:' + fmtm + r'}').format(f) + ' '
    
    estr += prefs[int(iee)]
        
    return estr

