我有一个浮点数,小数点后有300个数字。我该如何输入并打印该数字直到小数位?
示例:我有
pi_300 = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273
我接受输入,例如-15
我需要3.141592653589793
我接受输入,例如-20
我现在需要3.14159265358979323846
或
another_num = 3345.1234567890123456789012345678901234567890
我接受输入,例如-15
我需要3345.123456789012345
我接受输入,例如-18
我现在需要3345.123456789012345678
我尝试了format
方法,但是没有用
答案 0 :(得分:2)
浮点数的精度有限。当您执行操作时,会立即丢失大多数小数。看到这里:
>>> pi_300 = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594
08128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273
>>> print(pi_300)
3.141592653589793
无法从该数字中获取更多数字,因为它们已经丢失。
现在,如果要提高精度,可以使用Decimal
:
>>> from decimal import Decimal
>>> pi_300 = Decimal('3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822
3172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914
1273')
>>> print(pi_300)
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745
0284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273
您现在可以将其舍入为15位小数:
>>> print(round(pi_300, 15))
3.141592653589793
或更多:
>>> print(round(pi_300, 25))
3.1415926535897932384626434
请注意,如果它超出了decimal
模块所定义的精度,则位数可能会失败:
>>> print(round(pi_300, 45))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]
但这也可以配置:
>>> import decimal
>>> decimal.setcontext(decimal.Context(prec=100))
>>> print(round(pi_300, 45))
3.141592653589793238462643383279502884197169399
答案 1 :(得分:1)
两次使用格式迷你语言-一次创建格式字符串,一次进行打印:
from decimal import Decimal
pi_300 = Decimal("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273")
def printWith(num, wholeNum, decimNum):
"""Creates a format string and prints it"""
# create format string
f = "{{:{}.{}f}}".format(wholeNum,decimNum+1) # 1 decimal more then needed
t = f.format(num)[:-1] # remove the last (maybe rounded) digit
# print outout (or better return it)
print ( t )
printWith(pi_300,2,15)
输出:
3.141592653589793
答案 2 :(得分:0)
格式似乎对我有用。这是我尝试过的。
>>>'{0:.6f}'.format(pi_300)
>>>'3.141593'
>>>'{0:.2f}'.format(pi_300)
>>>'3.14'
根据小括号中的点后的数字更改小数。它的格式语法比%
稍新。请注意,格式化它会自动舍入数字。
>>> round(pi_300, 3)
3.142
>>> '{0:.3f}'.format(pi_300)
'3.142'
所以,这里:
>>> def print_rounded(num, to):
... return '{}'.format(round(num, to))
...
>>> print_rounded(pi_300, 3)
'3.142'