十进制和主干 [英] decimal and trunkating

查看:75
本文介绍了十进制和主干的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将199.999转为199.99

getcontext.prec = 2不是我之后的任何一个,所有这一切都是E'的

值。

我真的必须使用浮点数来做这个吗?

i want to trunkate 199.999 to 199.99
getcontext.prec = 2 isn''t what i''m after either, all that does is E''s
the value.
do i really have to use floats to do this?

推荐答案

Le Thu,2005年6月2日19:59:08 +1000,Timothy Smithaécrit:
Le Thu, 02 Jun 2005 19:59:08 +1000, Timothy Smith a écrit :
我想将199.999加入199.99


round(199.999,2)#2位数后小数点

我真的需要用浮点数来做这个吗?
i want to trunkate 199.999 to 199.99
round(199.999, 2) # 2 digits after the decimal point
do i really have to use floats to do this?




19.999是一个浮点数:

type(19.999)是float#==>真的



19.999 is a float :
type(19.999) is float # ==> True


F. Petitjean写道:
F. Petitjean wrote:
Le Thu,2005年6月2日19:59:08 +1000,Timothy Smithaécrit:
Le Thu, 02 Jun 2005 19:59:08 +1000, Timothy Smith a écrit :
我想将199.999加密到199.99
i want to trunkate 199.999 to 199.99

<圆形(199.999,2)小数点后#2位数



round(199.999, 2) # 2 digits after the decimal point




错误。这将产生200.00。



Wrong. This will yield 200.00.

我真的必须使用浮点数来做这个吗?
do i really have to use floats to do this?



19.999是浮点数:
类型(19.999)是浮点数#==>真的



19.999 is a float :
type(19.999) is float # ==> True




他说的是小数...


d =十进制(" 199.999")

d._round(5,decimal.ROUND_DOWN)


Reinhold



He is speaking of Decimals...

d = Decimal("199.999")
d._round(5, decimal.ROUND_DOWN)

Reinhold


Timothy Smith< ti *** **@open-networks.net>写道:
Timothy Smith <ti*****@open-networks.net> wrote:
我想将199.999加密到199.99
getcontext.prec = 2不是我之后的任何一个,所有这一切都是E'的
值得。
我真的必须使用浮点数来做这个吗?
i want to trunkate 199.999 to 199.99
getcontext.prec = 2 isn''t what i''m after either, all that does is E''s
the value.
do i really have to use floats to do this?




您可以尝试这个(从我用于手机账单的脚本):


来自十进制导入十进制为d


def roundDecimal(num,prec):

返回d(num ).quantize(d(1e%d%( - prec)))

其中`prec`是小数点后的位数。


我肯定有更好的解决方案,有人会告诉它,因此

教我们两个。 ;-)


Adia?*,Marc



You could try this (from a script I use for my phone bill):

from decimal import Decimal as d

def roundDecimal(num, prec):
return d(num).quantize(d("1e%d" % (-prec)))

where `prec` is the number of places after the decimal point.

I''m sure there is a better solutions and someone will tell it, thereby
teaching us both. ;-)

Adia?*, Marc


这篇关于十进制和主干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆