Python數字modf()方法

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)

上一篇: Python數字 下一篇: Python字串