Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有方法只返回除法后的小数?(蟒蛇3)_Python_Math_Division - Fatal编程技术网

Python 有没有方法只返回除法后的小数?(蟒蛇3)

Python 有没有方法只返回除法后的小数?(蟒蛇3),python,math,division,Python,Math,Division,在Python3中,是否有任何方法只返回除法后的十进制值 例如,如果在这个方法中我将4除以5,它将返回8。这个怎么样 >>> int(((4.0 / 5.0) % 1) * 10) 8 这个怎么样 >>> int(((4.0 / 5.0) % 1) * 10) 8 使用浮点除法。e、 g: (11.0 / 5) % 1 = 0.20000000000000018 (1.0 * 11 / 5) % 1 = 0.20000000000000018 使用浮点

在Python3中,是否有任何方法只返回除法后的十进制值

例如,如果在这个方法中我将4除以5,它将返回8。

这个怎么样

>>> int(((4.0 / 5.0) % 1) * 10)
8
这个怎么样

>>> int(((4.0 / 5.0) % 1) * 10)
8

使用浮点除法。e、 g:

(11.0 / 5) % 1 = 0.20000000000000018

(1.0 * 11 / 5) % 1 = 0.20000000000000018

使用浮点除法。e、 g:

(11.0 / 5) % 1 = 0.20000000000000018

(1.0 * 11 / 5) % 1 = 0.20000000000000018

对于一般数字
a
,可以使用
int
轻松计算:

int(10*a)%10
但是,请注意,这只适用于正数

如果您希望它也适用于负数:

int(10*a*cmp(a,0))%10*cmp(a,0)

对于一般数字
a
,可以使用
int
轻松计算:

int(10*a)%10
但是,请注意,这只适用于正数

如果您希望它也适用于负数:

int(10*a*cmp(a,0))%10*cmp(a,0)

不需要浮点运算就可以做到这一点,对吗

foo=lambda x,y: (10*(x%y))/y)

# Demo
>>> for i in range(20):
...   print(foo(i,7), i/7.0)
... 
(0, 0.0)
(1, 0.14285714285714285)
(2, 0.2857142857142857)
(4, 0.42857142857142855)
(5, 0.5714285714285714)
(7, 0.7142857142857143)
(8, 0.8571428571428571)
(0, 1.0)
(1, 1.1428571428571428)
(2, 1.2857142857142858)
(4, 1.4285714285714286)
(5, 1.5714285714285714)
(7, 1.7142857142857142)
(8, 1.8571428571428572)
(0, 2.0)
(1, 2.1428571428571428)
(2, 2.2857142857142856)
(4, 2.4285714285714284)
(5, 2.5714285714285716)
(7, 2.7142857142857144)

不需要浮点运算就可以做到这一点,对吗

foo=lambda x,y: (10*(x%y))/y)

# Demo
>>> for i in range(20):
...   print(foo(i,7), i/7.0)
... 
(0, 0.0)
(1, 0.14285714285714285)
(2, 0.2857142857142857)
(4, 0.42857142857142855)
(5, 0.5714285714285714)
(7, 0.7142857142857143)
(8, 0.8571428571428571)
(0, 1.0)
(1, 1.1428571428571428)
(2, 1.2857142857142858)
(4, 1.4285714285714286)
(5, 1.5714285714285714)
(7, 1.7142857142857142)
(8, 1.8571428571428572)
(0, 2.0)
(1, 2.1428571428571428)
(2, 2.2857142857142856)
(4, 2.4285714285714284)
(5, 2.5714285714285716)
(7, 2.7142857142857144)

如果你把1除以3呢?它是否应该返回3333(等)?还是只有3?如果你把1除以3呢?它是否应该返回3333(等)?或者仅仅3?添加一个
%10
,它实际上适用于所有适合数字类型的输入。除非所述值为负值。一定要小心模;)添加一个
%10
,它实际上适用于所有适合数字类型的输入。除非所述值为负值。一定要小心模;)