Python3 modf()函數

modf() 方法返回x的整數和小數部分,並放在元組中兩個專案。兩個部分與x的符號相同。整數部分返回為float。

語法

下麵是 modf() 方法的語法:
import math

math.modf( x )

注:此函數無法直接訪問,所以我們需要導入 math 模組,然後用數學靜態對象調用這個函數。

參數

  • x -- 這是一個數字運算式

返回值

此方法返回x的整數和小數部分並分別放在元組中的兩專案。 兩個部分與x的符號相同。整數部分返回為float。

示例

下麵的示例顯示 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)

上一篇: Python3迴圈 下一篇: Python3數字