2006年07月09日 星期日 17:26
怎么限制小数点, 比如说 >>> 5/6. 0.83333333333333337 我想就要0.83
2006年07月09日 星期日 18:18
On 7/9/06, linda. s <samrobertsmith at gmail.com> wrote: > 怎么限制小数点, > 比如说 > >>> 5/6. > 0.83333333333333337 > >>> "%.3f"%(5/6.0) '0.833' > 我想就要0.83 > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -- """Time is unimportant, only life important! blogging : http://blog.zoomquiet.org/pyblosxom/ wiki enter: http://wiki.woodpecker.org.cn/moin/ZoomQuiet in douban: http://www.douban.com/people/zoomq/ """
2006年07月09日 星期日 18:47
变成string了,如果我还是想要float,但同时想限制小数点? On 7/9/06, Zoom. Quiet <zoom.quiet at gmail.com> wrote: > On 7/9/06, linda. s <samrobertsmith at gmail.com> wrote: > > 怎么限制小数点, > > 比如说 > > >>> 5/6. > > 0.83333333333333337 > > > >>> "%.3f"%(5/6.0) > '0.833' > > > 我想就要0.83 > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > -- > """Time is unimportant, only life important! > blogging : http://blog.zoomquiet.org/pyblosxom/ > wiki enter: http://wiki.woodpecker.org.cn/moin/ZoomQuiet > in douban: http://www.douban.com/people/zoomq/ > """ > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > >
2006年07月09日 星期日 20:43
十进制数在python内部都是通过二进制数表达的,所以"不存在"0.833这样一个float数值: >>> 0.833 0.83299999999999996 这就是python内部float所能表达的最接近0.833的值。 如果你一定需要0.833(我觉得绝大多数时候其实并没有这个需求),就不能使用内建的float,要用别的自定义类型。 On 7/9/06, linda. s <samrobertsmith at gmail.com> wrote: > > 变成string了,如果我还是想要float,但同时想限制小数点? > > On 7/9/06, Zoom. Quiet <zoom.quiet at gmail.com> wrote: > > On 7/9/06, linda. s <samrobertsmith at gmail.com> wrote: > > > 怎么限制小数点, > > > 比如说 > > > >>> 5/6. > > > 0.83333333333333337 > > > > > >>> "%.3f"%(5/6.0) > > '0.833' > > > > > 我想就要0.83 > > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060709/27219c5b/attachment.html
2006年07月09日 星期日 20:56
import decimal On 7/9/06, swordsp <sparas2006 at 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 at gmail.com> wrote: > > 变成string了,如果我还是想要float,但同时想限制小数点? > > > > On 7/9/06, Zoom. Quiet <zoom.quiet at gmail.com> wrote: > > > On 7/9/06, linda. s <samrobertsmith at gmail.com > wrote: > > > > 怎么限制小数点, > > > > 比如说 > > > > >>> 5/6. > > > > 0.83333333333333337 > > > > > > > >>> "%.3f"%(5/6.0) > > > '0.833' > > > > > > > 我想就要0.83 > > > > > > > > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to > python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to > python-chinese-request at lists.python.cn > Detail Info: > http://python.cn/mailman/listinfo/python-chinese > >
2006年07月12日 星期三 14:38
引自shhgs的帖子"Py2.5 What's new 之 with" (搜索前贴能找到)
==================================================
...
decimal模块提供了一个能返回context对象的context_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 at gmail.com> wrote:
>
> import decimal
>
> On 7/9/06, swordsp <sparas2006 at 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 at gmail.com> wrote:
> > > 变成string了,如果我还是想要float,但同时想限制小数点?
> > >
> > > On 7/9/06, Zoom. Quiet <zoom.quiet at gmail.com> wrote:
> > > > On 7/9/06, linda. s <samrobertsmith at gmail.com > wrote:
> > > > > 怎么限制小数点,
> > > > > 比如说
> > > > > >>> 5/6.
> > > > > 0.83333333333333337
> > > > >
> > > > >>> "%.3f"%(5/6.0)
> > > > '0.833'
> > > >
> > > > > 我想就要0.83
> > > > >
> > >
> > >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to
> > python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request at lists.python.cn
> > Detail Info:
> > http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060712/8ecb2c1f/attachment.html
Zeuux © 2025
京ICP备05028076号