Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express floating point numbers in non standard notation in python?

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

like image 965
anijhaw Avatar asked Feb 19 '26 05:02

anijhaw


1 Answers

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.

like image 100
tpg2114 Avatar answered Feb 20 '26 17:02

tpg2114



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!