是否存在将数字分割1234.5678
成两个部分((1234, 0.5678)
即整数部分和小数部分)的有效方法?
Answers:
用途math.modf
:
import math
x = 1234.5678
math.modf(x) # (0.5678000000000338, 1234.0)
int
用作变量名,它会覆盖该int
函数。
int_
如果您必须具有一个大声读取时称为“ int”的变量,请使用。
我们可以使用一个不著名的内置函数;divmod:
>>> s = 1234.5678
>>> i, d = divmod(s, 1)
>>> i
1234.0
>>> d
0.5678000000000338
divmod(-4.5,1)
给出-5.0和0.5。使用divmod(-4.5, -1)
给出4.0和-0.5。
>>> a = 147.234
>>> a % 1
0.23400000000000887
>>> a // 1
147.0
>>>
如果您希望整数部分为整数而不是浮点数,请int(a//1)
改用。要在单个段落中获取元组:(int(a//1), a%1)
编辑:请记住,浮点数的小数部分是近似值,因此,如果您要像人类那样表示它,则需要使用十进制库
-2.25 // 1 == -3.0
和的结果会造成混淆-2.25 % 1 == 0.75
。这可能是OP想要的,因为int部分+小数部分仍然等于原始值。相反,math.modf(-2.25) == (-0.25, -2.0)
。
intpart,decimalpart = int(value),value-int(value)
适用于正数。
In [1]: value = 1.89
In [2]: intpart,decimalpart = int(value),value-int(value)
In [3]: intpart
Out [3]: 1
In [4]: decimalpart
Out [4]: 0.8899999999999999
看了几个答案之后。我提出了这两个语句,它们可以将正数和负数分为整数部分和小数部分,而不会影响精度。性能测试表明,math.modf
只要不将这两个新语句放入自己的函数或方法中,它们就会比快。
i = int(x) # i contains a positive or negative integer
f = (x*1e17-i*1e17)/1e17 # f contains a positive or negative fraction
例如100.1323
->100, 0.1323
和-100.1323
->-100, -0.1323
测试脚本:
#!/usr/bin/env python
import math
import cProfile
""" Get the performance of both statements vs math.modf. """
X = -100.1323
LOOPS = range(5*10**6)
def fun_a():
""" The integer part (i) is an integer, and
the fraction part (f) is a float.
NOTE: I think this is the most correct way. """
for _ in LOOPS:
i = int(X) # -100
f = (X*1e17-i*1e17)/1e17 # -0.1323
def fun_b():
""" The integer (i) and fraction (f) part will
come out as float.
NOTE: The only difference between this
and math.modf is the accuracy. """
for _ in LOOPS:
i = int(X) # -100
i, f = float(i), (X*1e17-i*1e17)/1e17 # (-100.0, -0.1323)
def fun_c():
""" Performance test of the statements in a function.
The integer part (i) is an integer, and
the fraction part (f) is a float. """
def modf(x):
i = int(x)
return i, (x*1e17-i*1e17)/1e17
for _ in LOOPS:
i, f = modf(X) # (-100, -0.1323)
def fun_d():
for _ in LOOPS:
f, i = math.modf(X) # (-100.0, -0.13230000000000075)
def fun_e():
""" Convert the integer part to integer. """
for _ in LOOPS:
f, i = math.modf(X) # (-100.0, -0.13230000000000075)
i = int(i) # -100
if __name__ == '__main__':
cProfile.run('fun_a()')
cProfile.run('fun_b()')
cProfile.run('fun_c()')
cProfile.run('fun_d()')
cProfile.run('fun_e()')
输出:
4 function calls in 1.312 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 1.312 1.312 <string>:1(<module>)
1 1.312 1.312 1.312 1.312 new1.py:10(fun_a)
1 0.000 0.000 1.312 1.312 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
4 function calls in 1.887 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 1.887 1.887 <string>:1(<module>)
1 1.887 1.887 1.887 1.887 new1.py:17(fun_b)
1 0.000 0.000 1.887 1.887 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
5000004 function calls in 2.797 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 2.797 2.797 <string>:1(<module>)
1 1.261 1.261 2.797 2.797 new1.py:23(fun_c)
5000000 1.536 0.000 1.536 0.000 new1.py:27(modf)
1 0.000 0.000 2.797 2.797 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
5000004 function calls in 1.852 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 1.852 1.852 <string>:1(<module>)
1 1.050 1.050 1.852 1.852 new1.py:34(fun_d)
1 0.000 0.000 1.852 1.852 {built-in method builtins.exec}
5000000 0.802 0.000 0.802 0.000 {built-in method math.modf}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
5000004 function calls in 2.467 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 2.467 2.467 <string>:1(<module>)
1 1.652 1.652 2.467 2.467 new1.py:38(fun_e)
1 0.000 0.000 2.467 2.467 {built-in method builtins.exec}
5000000 0.815 0.000 0.815 0.000 {built-in method math.modf}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
注意:
使用模数语句可以更快,但是模数不能用于将负数拆分为整数和小数部分。
i, f = int(x), x*1e17%1e17/1e17 # x can not be negative