Skip to content

Instantly share code, notes, and snippets.

@aseeon
Created September 4, 2014 13:57
Show Gist options
  • Save aseeon/3f06d95f995fde7adfc2 to your computer and use it in GitHub Desktop.
Save aseeon/3f06d95f995fde7adfc2 to your computer and use it in GitHub Desktop.
Find e to the Nth Digit. Enter a number and have the program generate e up to that many decimal places. Keep a limit to how far the program will go
""""Find e to the Nth Digit
Enter a number and have the program generate e up to that many decimal places.
Keep a limit to how far the program will go"""
from math import e
def e_with_precision(n):
"""Return euler's number to the n-th decimal place
:param n: number of decimal places to return
:type n: int
:return: euler's number with n decimal places
:rtype: str
"""
return '%.*f' % (n, e)
if __name__ == '__main__':
# there is no do while loop in python, so we need to improvise
correct_input = False
while not correct_input:
# ask until you get correct input
print('Precision must be between 1 and 51')
precision = int(raw_input('Number of decimal places: '))
if 51 >= precision > 0:
correct_input = True
print(e_with_precision(precision))
@mbomb007
Copy link

The output is incorrect for n > 15 .
https://www.math.utah.edu/~pa/math/e.html.

.....Your output for n=50 with spaces added: 2.71828 18284 59045 09079 55982 98427 64884 23347 47314 45312
Correct output for n=50 with spaces added: 2.71828 18284 59045 23536 02874 71352 66249 77572 47093 69995

@elidot
Copy link

elidot commented Apr 24, 2018

return '%.*f' % (n,e).

hi, I tried to use the new formatting for this but I could not get it right, i'm new to this, so I was wondering if there is way to use the new formatting in python 3 instead of %.

thank you in advance

@Athul8raj
Copy link

You can put placeholders "{ :*f}."format(n,e) in python 3 or using f string formatting in python 3.6...both works fine

@patyyyo
Copy link

patyyyo commented Aug 14, 2019

Athul8raj I've tried using the placeholders in your comment but there not working.

@yashpathare21
Copy link

Please provide an alternative to this return '%.*f' % (n, e) for python 3

@yashpathare21
Copy link

Please provide an alternative to this return '%.*f' % (n, e) for python 3

Which is easy to read and understand as it don't know anything about python 2.

@slysz
Copy link

slysz commented Mar 2, 2021

Please provide an alternative to this return '%.*f' % (n, e) for python 3

This works perfectly well on Python 3. So just learn how different formatting styles work -> https://pyformat.info/ and rewrite it yourself.

@yashpathare21
Copy link

I meant can you give an alternative to this return '%.*f' % (n, e) in .format() or f-string method. As I know how these methods work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment