0

I want to display the value 6.1234567891e-05 as 6.123e-05 using python I tried using function round(). But its giving me wrong answer. Please help me with a solution.

Regards, Sneha

  • 1
    possible duplicate of [python limiting floats to two decimal points](http://stackoverflow.com/questions/455612/python-limiting-floats-to-two-decimal-points) – Roney Michael Aug 20 '14 at 06:25
  • Please see the [Python string formatting](https://docs.python.org/2/library/string.html#formatspec). –  Aug 20 '14 at 06:29

2 Answers2

0

You need to use string formatting.

n = 6.1234567891e-05
print '%1.3e'%n

Take a look at this for a quick reference: https://mail.python.org/pipermail/tutor/2008-June/062649.html

user2963623
  • 2,267
  • 1
  • 14
  • 25
0

You can set the number of decimals to print without changing the model of the data but just changing the view. For example if you want only three numbers after the dot you can use:

'%.3e' % n

Example

>>> print '%.3e' % 0.000061234567891
6.123e-05
enrico.bacis
  • 30,497
  • 10
  • 86
  • 115