Python数字modf()
方法返回两个元组元素分别来表示x
的小数和整数部分。 两个部分与x
具有相同的符号。整数部分作为浮点数返回。
语法
以下是modf()
方法的语法 -
import math
math.modf( x )
注意 - 此函数不可直接访问,因此需要导入
math
模块,然后需要使用math
静态对象调用此函数。
参数
- x - 这是一个数字表达式。
返回值
- 该方法返回包含两个元素的元组,它们分别表示
x
的小数和整数部分。 这两个部分的符号与x
相同。整数部分作为浮点数返回。
示例
以下示例显示了modf()
方法的用法。
#!/usr/bin/python3
import math # This will import math module
print ("math.modf(100.12) : ", math.modf(100.12))
print ("math.modf(100.72) : ", math.modf(100.72))
print ("math.modf(119) : ", math.modf(119))
print ("math.modf(math.pi) : ", math.modf(math.pi))
当运行上述程序时,它会产生以下结果 -
math.modf(100.12) : (0.12000000000000455, 100.0)
math.modf(100.72) : (0.7199999999999989, 100.0)
math.modf(119) : (0.0, 119.0)
math.modf(math.pi) : (0.14159265358979312, 3.0)