Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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))
@daniel-chae

This comment has been minimized.

Copy link

commented Jul 3, 2015

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

I was looking at your problem solving and I couldn't understand what the above sentence does.
Could you explain it for me ??

@srm01

This comment has been minimized.

Copy link

commented Aug 17, 2015

return '%.*f' % (n,e) , returns the string type of nth value of e ,toned down to precision n. When the precision is not known, until before runtime, this syntax works fine in that case.

@mbomb007

This comment has been minimized.

Copy link

commented Oct 25, 2016

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented May 16, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.