Python modf() Function

Python 数字Python Numbers


Description

The modf() method returns the integer part and fractional part of x. Both parts have the same sign as x, and the integer part is represented as a float.


Syntax

The following is the syntax for the modf() method:

import math

math.modf( x )

Note:modf() is not directly accessible; you need to import the math module and call the method through a static object.


Parameters

  • x -- A numeric expression.

Return Value

Returns the integer part and fractional part of x.

Example

The following example demonstrates the use of the modf() method:

#!/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)

The output of the above example after running is:

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)

Python 数字Python Numbers

Other Extensions