Latest web development tutorials

Python modf() 函數

Python 數字 Python數字


描述

modf() 方法返回x的整數部分與小數部分,兩部分的數值符號與x相同,整數部分以浮點型表示。


語法

以下是modf() 方法的語法:

import math

math.modf( x )

注意: modf()是不能直接訪問的,需要導入math模塊,通過靜態對象調用該方法。


參數

  • x -- 數值表達式。

返回值

返回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)

Python 數字 Python數字