Hướng dẫn does division round down in python? - phân chia có làm tròn xuống trong python không?

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
    
    0
  • 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
    
    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]]
# -2
2 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]]
# -2
3 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]]
# -2
2 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]]
# -2
5. 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]]
# -2
6 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]]
# -2
2 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 expression
    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 will first perform normal division and then skip the remainder—effectively “rounding
    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 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.
    : The
    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 function rounds number
    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 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 of
    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].
  • 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.
    : The
    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 function rounds number
    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 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 python
    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 lên và xuống đến số nguyên đầy đủ gần nhất.
    : The Python built-in
    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 function rounds
    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 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]]
# -2
2 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]]
# -2
3 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]]
# -2
5. 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ả.

Ví dụ:

  • print[int[42.52]]
    # 42
    
    print[int[21.99999]]
    # 21
    
    5
  • print[int[42.52]]
    # 42
    
    print[int[21.99999]]
    # 21
    
    6
  • print[int[42.52]]
    # 42
    
    print[int[21.99999]]
    # 21
    
    7
  • print[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]]
# -2
3 thực hiện phân chia số nguyên và toán tử
print[int[-0.1]]
# 0

print[int[-2]]
# -2
0 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]]
# -2
1. Một ví dụ cho phân chia phao là
print[int[-0.1]]
# 0

print[int[-2]]
# -2
2.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]]
# -2
3 operator performs integer division and the single-backslash
print[int[-0.1]]
# 0

print[int[-2]]
# -2
0 operator performs float division. An example for integer division is
print[int[-0.1]]
# 0

print[int[-2]]
# -2
1. An example for float division is
print[int[-0.1]]
# 0

print[int[-2]]
# -2
2.

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]]
# -2
3 với
print[int[-0.1]]
# 0

print[int[-2]]
# -2
4 và gọi
print[int[-0.1]]
# 0

print[int[-2]]
# -2
5.

Hàm trả về sàn của

print[int[-0.1]]
# 0

print[int[-2]]
# -2
6 đượ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]]
# -2
6.

💡 Lưu ý: Hàm

print[int[-0.1]]
# 0

print[int[-2]]
# -2
8 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]]
# -2
8 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]]
# -2
8 cũng như các chức năng
def round_down[x]:
    if x

Bài Viết Liên Quan

Chủ Đề