2006年09月13日 星期三 20:07
但是 >>> import decimal Traceback (most recent call last): File "", line 1, in -toplevel- import decimal ImportError: No module named decimal On 7/11/06, Bing Yuan <aceyuan在gmail.com> wrote: > 引自shhgs的帖子"Py2.5 What's new 之 with" (搜索前贴能找到) > ================================================== > ... > decimal模块提供了一个能返回context对象的con > text_manager()方法(请注意看注释, > 至少Py2.5b1里面是没有这个方法,你得用get_manager() ),这个context对象 > (context是上下文的意思)将计算的精度和近似要求封装了起来 > (什么叫上下文?这才叫上下文!Perl的那个上下文根本就是假冒伪劣): > > import decimal > > # Displays with default precision of 28 digits > v1 = decimal.Decimal('578') > print v1.sqrt() > > ctx = decimal.Context(prec=16) > #---------------------------------------- > # 根据shhgs的试验,ctx对象是没有context_manager()方法的, > # 你得 > # with ctx.get_manager() : > #---------------------------------------- > with ctx.context_manager(): > # All code in this block uses a precision of 16 digits. > # The original context is restored on exiting the block. > print v1.sqrt() > ... > =========================================== > > > On 7/9/06, shhgs <shhgs.efhilt在gmail.com> wrote: > > import decimal > > > > On 7/9/06, swordsp <sparas2006在gmail.com> wrote: > > > 十进制数在python内部都是通过二进制数表达的,所以"不存在"0.833这样一个float数值: > > > >>> 0.833 > > > 0.83299999999999996 > > > 这就是python内部float所能表达的最接近0.833的值。 > > > > > > > 如果你一定需要0.833(我觉得绝大多数时候其实并没有这个需求),就不能使用内建的float,要用别的自定义类型。 > > > > > > > > > On 7/9/06, linda. s < samrobertsmith在gmail.com> wrote: > > > > 变成string了,如果我还是想要float,但同时想限制小数点? > > > > > > > > On 7/9/06, Zoom. Quiet <zoom.quiet在gmail.com> wrote: > > > > > On 7/9/06, linda. s < samrobertsmith在gmail.com > wrote: > > > > > > 怎么限制小数点, > > > > > > 比如说 > > > > > > >>> 5/6. > > > > > > 0.83333333333333337 > > > > > > > > > > > >>> "%.3f"%(5/6.0) > > > > > '0.833' > > > > > > > > > > > 我想就要0.83 > > > > > > > > > > > > > > > > > > > > _______________________________________________ > > > python-chinese > > > Post: send python-chinese在lists.python.cn > > > Subscribe: send subscribe to > > > python-chinese-request在lists.python.cn > > > Unsubscribe: send unsubscribe to > > > python-chinese-request在lists.python.cn > > > Detail Info: > > > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese在lists.python.cn > > Subscribe: send subscribe to > python-chinese-request在lists.python.cn > > Unsubscribe: send unsubscribe to > python-chinese-request在lists.python.cn > > Detail Info: > http://python.cn/mailman/listinfo/python-chinese > > > > > > > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to > python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to > python-chinese-request在lists.python.cn > Detail Info: > http://python.cn/mailman/listinfo/python-chinese > >
2006年09月13日 星期三 20:13
On 9/13/06, linda. s <samrobertsmith在gmail.com> wrote: > 但是 > >>> import decimal > > Traceback (most recent call last): > File "", line 1, in -toplevel- > import decimal > ImportError: No module named decimal > decimal是2.4中才有的。 -- I like python! My Blog: http://www.donews.net/limodou UliPad Site: http://wiki.woodpecker.org.cn/moin/UliPad UliPad Maillist: http://groups.google.com/group/ulipad
2006年09月14日 星期四 14:31
同意swordsp所说的"不存在"0.833这样一个float数值。 这应该是计算机的基础知识吧。如果想要0.833,只有自己在转换了。 2006/9/13, limodou <limodou在gmail.com>: > On 9/13/06, linda. s <samrobertsmith在gmail.com> wrote: > > 但是 > > >>> import decimal > > > > Traceback (most recent call last): > > File "", line 1, in -toplevel- > > import decimal > > ImportError: No module named decimal > > > decimal是2.4中才有的。 > > -- > I like python! > My Blog: http://www.donews.net/limodou > UliPad Site: http://wiki.woodpecker.org.cn/moin/UliPad > UliPad Maillist: http://groups.google.com/group/ulipad > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese
Zeuux © 2025
京ICP备05028076号