Reputation: 9422
I am trying to print 1.2e-08 as 12e-09 for display purposes but cannot seem to get the format specifiers.
Is there a way to express floating point numbers in a non standard form without resorting to string operations or using regexes?
Edit: If the question is not clear, I am looking for something that lets me express any number in any scale for example if I want to scale everything in scale of 1e-12 then I should have something like 12000e-12
Thanks
Upvotes: 3
Views: 1354
Reputation: 15110
Check out the decimal module. It should be able to put it out in the format you want.
http://docs.python.org/library/decimal.html
For what you are describing (engineering notation), there is a to_eng_string() method.
EDIT
In light of your clarification, you can build the number in parts:
myNumber = 1.2e-8
myBase = 1e-12
mantissa = myNumber/myBase
print "{0:1}{1:s}".format(mantissa, "{0:1}".format(myBase)[1:])
Which would print: 12000.0e-12
If you knew you would always go to an integer number for the mantissa, you could adjust the format accordingly.
Upvotes: 6