Tom:
The file is attached. Place it in a suitable directory with the
name 'eng.py' and try 'python eng.py' again. It should print the
results of the tests.
When you are happy with it, add it to your python Calc macro.
Let me know if you still have trouble.
Dean
--
Dean Provins, P. Geoph.
[email protected]
KeyID at at pgpkeys.mit.edu:11371: 0x9643AE65
Fingerprint: 9B79 75FB 5C2B 22D0 6C8C 5A87 D579 9BE5 9643 AE65
# eng.py
#
def eng (F = 0, fmt = "%fe%d"):
"""
Formats a floating point number (F) according to the format
provided (fmt). Tries to use engineering notation (i.e. the
exponent is in powers of three).
Dean Provins, June, 2010
"""
f = abs (F)
n = 0
s = +1
if F < 0:
s = -1
if f != 0:
if f >= 1.:
while (f >= 1.):
f /= 10.
n += 1
f *= 10.
n -= 1
while (n % 3):
n += 1
f /= 10.
else:
while (f < 1.):
f *= 10.
n -= 1
while (n % 3):
n += 1
f /= 10.
# Uncomment these if you want a leading '0.'
#f /= 10.
#n += 1
#S = fmt % (s * f, n) # store this result in a cell
return fmt % (s * f, n) # return the formatted string to store in a cell
# ---------end of the function ---------
if __name__ == "__main__":
# Some tests... Try them by running "python eng.py"
print "some tests"
print "----------"
print "0:", eng ()
print
print "1.:", eng (1.)
print "1.23:", eng (1.23)
print "123:", eng (123)
print "1234.567:", eng (1234.567)
print
print "-1:", eng (-1.)
print "-1.23:", eng (-1.23)
print "-123:", eng (-123)
print "-1234.567:", eng (-1234.567)
print
print "11.:", eng (11.)
print
print "0.4:", eng (0.4)
print "-0.4:", eng (-0.4)
print
print "0.004:", eng (0.004)
print "0.04:", eng (-0.04)
print
print "0.001234567E-7:", eng (0.001234567E-7)
print "-0.001234567E-7:", eng (-0.001234567E-7)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]