无涯教程-Python - modf(x)函数

136 阅读1分钟

Python modf()返回两项元组中x的小数和整数部分。这两个部分都有与x相同的符号。

modf(x) - 语法

import math

math.modf( x )

  • x  -  这是一个数值表达式。

modf(x) - 返回值

此方法在两项元组中返回x的小数部分和整数部分。这两个部分都有与x相同的符号。整数部分作为浮点数返回。

modf(x) - 示例

以下示例显示modf()方法的用法。

#!/usr/bin/python
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(119L) : ", math.modf(119L) print "math.modf(math.pi) : ", math.modf(math.pi)

当无涯教程运行上面的程序时,它产生以下输出-

math.modf(100.12) :  (0.12000000000000455, 100.0)
math.modf(100.72) :  (0.71999999999999886, 100.0)
math.modf(119L) :    (0.0, 119.0)
math.modf(math.pi) :  (0.14159265358979312, 3.0)

参考链接

www.learnfk.com/python/numb…