Skip to main content
 首页 » 编程设计

python之程序显示为几位数

2025年05月04日13luoye11

该程序计算 2 的平方根,但它不会以我希望的 1000 位精度显示数字,而是只显示 50 位数字。

这是程序

import math  
a=format(2.0**0.5, '.1000')    
print a 

我想知道如何编写一个程序来显示更多的数字。

请您参考如下方法:

使用 decimal模块:

>>> from decimal import * 
>>> getcontext().prec = 100 
>>> Decimal(2).sqrt() 
Decimal('1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641573') 
>>> getcontext().prec = 1000 
>>> Decimal(2).sqrt() 
Decimal('1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126 
441214970999358314132226659275055927557999505011527820605714701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279 
952514079896872533965463318088296406206152583523950547457502877599617298355752203375318570113543746034084988471603868999706990048150305440277903164542 
478230684929369186215805784631115966687130130156185689872372352885092648612494977154218334204285686060146824720771435854874155657069677653720226485447 
015858801620758474922657226002085584466521458398893944370926591800311388246468157082630100594858704003186480342194897278290641045072636881313739855256 
117322040245091227700226941127573627280495738108967504018369868368450725799364729060762996941380475654823728997180326802474420629269124859052181004459 
84215059112024944134172853147810580360337107730918286931471017111168391658172688941975871658215212822951848847')