Xây dựng vấn đề: Cho một số float. Làm thế nào để làm tròn phao xuống trong Python?: Given a float number. How to round the float down in Python?
Dưới đây là một số ví dụ về những gì bạn muốn thực hiện:
42.52 --> 42
21.99999 --> 22
import math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -2
0import math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -2
1
Giải pháp: Nếu bạn có ít thời gian, thì đây là câu trả lời đơn giản nhất:: If you have little time, here’s the most straightforward answer:
Để làm tròn một số dương hoặc âm
import math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -22 xuống trong Python, áp dụng phân chia số nguyên
import math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -23 thành
import math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -22 và chia cho
import math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -25. Cụ thể, biểu thức
import math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -26 trước tiên sẽ thực hiện phân chia phao bình thường và sau đó vứt bỏ phần còn lại, một cách hiệu quả.
Nói chung, có nhiều cách để làm tròn số float
import math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -22 xuống trong Python:
- Vanilla Python: Biểu thức
import math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -2
6 trước tiên sẽ thực hiện phân chia bình thường và sau đó bỏ qua phần còn lại, một cách hiệu quả.: The expressionimport math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -2
6 will first perform normal division and then skip the remainder—effectively “roundingimport math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -2
2 down”. - Làm tròn xuống: Hàm
import numpy as np print[np.floor[42.52]] # 42.0 print[np.floor[21.99999]] # 21.0 print[np.floor[-0.1]] # -1.0 print[np.floor[-2]] # -2.0
1 làm tròn sốimport math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -2
2 cho đến số nguyên đầy đủ tiếp theo.: Theimport numpy as np print[np.floor[42.52]] # 42.0 print[np.floor[21.99999]] # 21.0 print[np.floor[-0.1]] # -1.0 print[np.floor[-2]] # -2.0
1 function rounds numberimport math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -2
2 down to the next full integer. - Làm tròn xuống [biểu diễn phao]: Thay vào đó,
import numpy as np print[np.floor[42.52]] # 42.0 print[np.floor[21.99999]] # 21.0 print[np.floor[-0.1]] # -1.0 print[np.floor[-2]] # -2.0
3 làm tròn xuống và trả về một biểu diễn phao của số nguyên đầy đủ tiếp theo [ví dụ:import numpy as np print[np.floor[42.52]] # 42.0 print[np.floor[21.99999]] # 21.0 print[np.floor[-0.1]] # -1.0 print[np.floor[-2]] # -2.0
4 thay vìimport numpy as np print[np.floor[42.52]] # 42.0 print[np.floor[21.99999]] # 21.0 print[np.floor[-0.1]] # -1.0 print[np.floor[-2]] # -2.0
5].: Alternatively,import numpy as np print[np.floor[42.52]] # 42.0 print[np.floor[21.99999]] # 21.0 print[np.floor[-0.1]] # -1.0 print[np.floor[-2]] # -2.0
3 rounds down and returns a float representation of the next full integer [e.g.,import numpy as np print[np.floor[42.52]] # 42.0 print[np.floor[21.99999]] # 21.0 print[np.floor[-0.1]] # -1.0 print[np.floor[-2]] # -2.0
4 instead ofimport numpy as np print[np.floor[42.52]] # 42.0 print[np.floor[21.99999]] # 21.0 print[np.floor[-0.1]] # -1.0 print[np.floor[-2]] # -2.0
5]. - Làm tròn lên: Hàm
import numpy as np print[np.floor[42.52]] # 42.0 print[np.floor[21.99999]] # 21.0 print[np.floor[-0.1]] # -1.0 print[np.floor[-2]] # -2.0
6 làm tròn sốimport math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -2
2 cho đến số nguyên đầy đủ tiếp theo.: Theimport numpy as np print[np.floor[42.52]] # 42.0 print[np.floor[21.99999]] # 21.0 print[np.floor[-0.1]] # -1.0 print[np.floor[-2]] # -2.0
6 function rounds numberimport math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -2
2 up to the next full integer. - Làm tròn lên và xuống: Hàm
import numpy as np print[np.floor[42.52]] # 42.0 print[np.floor[21.99999]] # 21.0 print[np.floor[-0.1]] # -1.0 print[np.floor[-2]] # -2.0
8 được tích hợp pythonimport math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -2
2 lên và xuống đến số nguyên đầy đủ gần nhất.: The Python built-inimport numpy as np print[np.floor[42.52]] # 42.0 print[np.floor[21.99999]] # 21.0 print[np.floor[-0.1]] # -1.0 print[np.floor[-2]] # -2.0
8 function roundsimport math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -2
2 up and down to the closest full integer.
Hãy cùng đi sâu vào từng người và nhiều lựa chọn hơn trong bài viết còn lại. Tôi đảm bảo bạn sẽ thoát khỏi nó đã học được ít nhất một vài thủ thuật Python mới trong quá trình này!
Phương pháp 1: Phân chia số nguyên [X // 1]
Cách đơn giản nhất để làm tròn số dương hoặc âm
import math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -22 xuống trong Python là sử dụng phân chia số nguyên
import math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -23 bởi
import math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -25. Biểu thức
import math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -26 trước tiên sẽ thực hiện phân chia bình thường và sau đó bỏ qua phần còn lại, một cách hiệu quả.
Ví dụ:
print[int[42.52]] # 42 print[int[21.99999]] # 21
5print[int[42.52]] # 42 print[int[21.99999]] # 21
6print[int[42.52]] # 42 print[int[21.99999]] # 21
7print[int[42.52]] # 42 print[int[21.99999]] # 21
8
Thủ thuật này hoạt động cho những con số tích cực và tiêu cực. 🌻
Dưới đây là một vài ví dụ về mã Python:
def round_down[x]: return x//1 print[round_down[42.52]] # 42 print[round_down[21.99999]] # 21 print[round_down[-0.1]] # -1 print[round_down[-2]] # -2
🎓 Thông tin: Toán tử Double-Backslash
import math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -23 thực hiện phân chia số nguyên và toán tử
print[int[-0.1]] # 0 print[int[-2]] # -20 duy nhất thực hiện phân chia float. Một ví dụ cho phân chia số nguyên là
print[int[-0.1]] # 0 print[int[-2]] # -21. Một ví dụ cho phân chia phao là
print[int[-0.1]] # 0 print[int[-2]] # -22.Info: The double-backslash
import math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -23 operator performs integer division and the single-backslash
print[int[-0.1]] # 0 print[int[-2]] # -20 operator performs float division. An example for integer division is
print[int[-0.1]] # 0 print[int[-2]] # -21. An example for float division is
print[int[-0.1]] # 0 print[int[-2]] # -22.
Hãy xem video sau đây để biết sự lặp lại hoặc học tập:
Python Division Deep Dive
Phương pháp 2: Math.Floor []
Để làm tròn một số xuống trong Python, hãy nhập thư viện
print[int[-0.1]] # 0 print[int[-2]] # -23 với
print[int[-0.1]] # 0 print[int[-2]] # -24 và gọi
print[int[-0.1]] # 0 print[int[-2]] # -25.
Hàm trả về sàn của
print[int[-0.1]] # 0 print[int[-2]] # -26 được chỉ định được định nghĩa là số nguyên lớn nhất nhỏ hơn hoặc bằng
print[int[-0.1]] # 0 print[int[-2]] # -26.
💡 Lưu ý: Hàm
print[int[-0.1]] # 0 print[int[-2]] # -28 làm tròn chính xác sự nổi lên số nguyên đầy đủ tiếp theo cho các số nguyên dương và âm.Note: The
print[int[-0.1]] # 0 print[int[-2]] # -28 function correctly rounds down floats to the next-smaller full integer for positive and negative integers.
Dưới đây, một ví dụ mã làm tròn năm số của chúng tôi xuống số nguyên đầy đủ tiếp theo:
import math print[math.floor[42.52]] # 42 print[math.floor[21.99999]] # 21 print[math.floor[-0.1]] # -1 print[math.floor[-2]] # -2
Video sau đây cho thấy
print[int[-0.1]] # 0 print[int[-2]] # -28 cũng như các chức năng
def round_down[x]: if x