我們在 Python 中使用了不同的數值數據類型,在本教程中,我們將學習如何將浮點值轉換為整數值。
讓我們看看實現相同的方法-
- 使用 trunc()
- 使用樓層()
- 使用 ceil()
- 使用 int()
所以,讓我們從第一個開始-
使用 trunc()
下面的程序展示了如何在 Python 中使用 trunc() 將浮點值轉換為整數。
#import trunc
from math import trunc
#initialising the values
a = 20.33
b = 12.46
c = 9.54
res_sum = a + b + c
#displaying the sum value
print("The result of a + b + c is ", res_sum)
#using trunc
print("The converted value of a is: ", trunc(a))
print("The converted value of b is: ", trunc(b))
print("The converted value of c is: ", trunc(c))
print("The converted value of sum is: ", trunc(res_sum))
輸出:
The result of a + b + c is 42.33
The converted value of a is: 20
The converted value of b is: 12
The converted value of c is: 9
The converted value of sum is: 42
解釋-
是時候了解上面的程序發生了什麼-
- 既然要用 trunc(),我們第一步就導入了數學。
- 之後,我們初始化了三個浮點值,然後計算它們的總和。
- 最後,我們傳遞了變數 trunc() 中的 a、b、c 和 res_sum 來獲得整數值。
- 在執行程序時,我們獲得了所需的輸出。
在下一個程序中,我們將使用 floor()。
使用地板()
首先,讓我們理解當我們在 floor()中傳遞一個浮點值時會發生什麼?
當一個浮點數傳入 floor() 時,它將該數向下舍入到最接近的整數。
考慮下面給出的程序,
#import floor
from math import floor
#initialising the values
a = 20.33
b = 12.46
c = 9.54
res_sum = a + b + c
#displaying the sum value
print("The result of a + b + c is ", res_sum)
#using floor
print("The converted value of a is: ", floor(a))
print("The converted value of b is: ", floor(b))
print("The converted value of c is: ", floor(c))
print("The converted value of sum is: ", floor(res_sum))
輸出:
The result of a + b + c is 42.33
The converted value of a is: 20
The converted value of b is: 12
The converted value of c is: 9
The converted value of sum is: 42
解釋-
讓我們看一下這個節目的解說。
- 既然要用 floor(),第一步就導入了數學。
- 之後,我們初始化了三個浮點值,然後計算它們的總和。
- 最後,我們在樓層()傳遞了變數 a、b、c、和 res_sum 得到整數值。
- 在執行程序時,我們獲得了所需的輸出。
現在,我們將看到如何使用 ceil() 來做同樣的事情。
使用天花板()
首先,讓我們理解當我們在 ceil()中傳遞一個浮點值時會發生什麼?
當一個浮點數傳入 ceil(),時,它將該數四捨五入到最接近的整數。
#import ceil
from math import ceil
#initialising the values
a = 20.33
b = 12.46
c = 9.54
res_sum = a + b + c
#displaying the sum value
print("The result of a + b + c is ", res_sum)
#using ceil
print("The converted value of a is: ", ceil(a))
print("The converted value of b is: ", ceil(b))
print("The converted value of c is: ", ceil(c))
print("The converted value of sum is: ", ceil(res_sum))
輸出:
The result of a + b + c is 42.33
The converted value of a is: 21
The converted value of b is: 13
The converted value of c is: 10
The converted value of sum is: 43
解釋-
讓我們了解一下我們在這個項目中做了什麼。
- 既然要用 ceil(),第一步就導入了數學。
- 之後,我們初始化了三個浮點值,然後計算它們的總和。
- 最後,我們通過變數 ceil() 中的 a、b、c、和 res_sum 得到整數值。
- 在執行程序時,我們獲得了所需的輸出。
最後,在最後一個程序中,我們將使用最基本的方法將浮點值轉換為整數,即使用 int() 。
使用 int()
下面給出的程序說明了如何在程序中使用它。
#initialising the values
a = 20.33
b = 12.46
c = 9.54
res_sum = a + b + c
#displaying the sum value
print("The result of a + b + c is ", res_sum)
#using int()
print("The converted value of a is: ", int(a))
print("The converted value of b is: ", int(b))
print("The converted value of c is: ", int(c))
print("The converted value of sum is: ", int(res_sum))
輸出:
The result of a + b + c is 42.33
The converted value of a is: 20
The converted value of b is: 12
The converted value of c is: 9
The converted value of sum is: 42
解釋-
- 我們已經初始化了三個浮點值,然後計算它們的總和。
- 最後,我們已經在 int() 中傳遞了變數 a、b、c、和 res_sum,以獲得整數值。
- 在執行程序時,我們獲得了所需的輸出。
結論
在本教程中,我們學習了在 Python 中將浮點數轉換為整數的有趣方法。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/242371.html