Hướng dẫn how do you round up decimals in python? - làm thế nào để bạn làm tròn số thập phân trong python?

Python có một số tùy chọn để làm tròn đến một số toàn bộ. Nhưng điều gì sẽ xảy ra nếu chúng ta muốn làm tròn đến một số chữ số thập phân, như 2 chữ số thập phân? Hãy để xem cách chúng ta làm điều đó.

Trong bài viết này:

  • Giá trị python tròn cho hai vị trí số thập phân (và hơn thế nữa)
  • Vòng thập phân tròn lên xuống:
    Value:          Rounded:
    3.14159265359   3.14159
    1845.7409947    1845.741
    -100.95         -100.95
    9.5432          9.54
    34.49953        34.5
    
    5
    • Ví dụ: Các chữ số thập phân tròn lên xuống
  • Vòng thập phân ở Python
    • Ví dụ: Làm tròn đến 2 chữ số thập phân
  • Vòng thập phân tròn xuống ở Python
    • Ví dụ: Làm tròn xuống 2 chữ số thập phân
  • Vòng thập phân của danh sách và mảng
    • Danh sách các vòng đến các vị trí thập phân với danh sách hiểu biết
    • Danh sách tròn đến các vị trí thập phân với vòng lặp Python
    • Mảng Python tròn đến 2 chữ số thập phân
    • Vòng numpy mảng đến hai chữ số thập phân
  • Bản tóm tắt

# Vòng Python giá trị đến hai vị trí số thập phân (và hơn thế nữa)

Mặc dù làm tròn vào một số toàn bộ là hữu ích, nhưng vẫn có rất nhiều tình huống mà chúng ta cần phải làm tròn đến một số vị trí thập phân nhất định. Ví dụ, tiền tệ được báo cáo tốt nhất với 2 chữ số thập phân. Và mức độ lỗi và ý nghĩa thường sử dụng nhiều chữ số hơn, như 5.

Python có một số cách để các chữ số thập phân tròn:

  • Hàm
    Value:          Rounded:
    3.14159265359   3.14159
    1845.7409947    1845.741
    -100.95         -100.95
    9.5432          9.54
    34.49953        34.5
    
    5 làm tròn số thập phân lên xuống. Điều này làm cho
    Value:          Rounded:
    3.14159265359   3.14159
    1845.7409947    1845.741
    -100.95         -100.95
    9.5432          9.54
    34.49953        34.5
    
    8 thành
    Value:          Rounded:
    3.14159265359   3.14159
    1845.7409947    1845.741
    -100.95         -100.95
    9.5432          9.54
    34.49953        34.5
    
    9 và
    round_decimals_up(8.343)
    # Returns: 8.35
    
    0 thành
    round_decimals_up(8.343)
    # Returns: 8.35
    
    1.
  • Để làm tròn các vị trí thập phân, chúng ta phải sử dụng một chức năng tùy chỉnh. Theo cách đó
    round_decimals_up(8.343)
    # Returns: 8.35
    
    2 trở thành
    round_decimals_up(8.343)
    # Returns: 8.35
    
    3.
  • Để làm tròn các vị trí thập phân xuống, chúng tôi sử dụng một chức năng tùy chỉnh. Điều đó biến
    round_decimals_up(8.343)
    # Returns: 8.35
    
    4 thành
    round_decimals_up(8.343)
    # Returns: 8.35
    
    5.

Hãy cùng xem xét từng cách tiếp cận và mã Python mà họ yêu cầu.

# Vòng thập phân vị trí lên xuống: Value: Rounded: 3.14159265359 3.14159 1845.7409947 1845.741 -100.95 -100.95 9.5432 9.54 34.49953 34.5 5

Với chức năng Python từ

Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
5, chúng ta có thể làm tròn các vị trí thập phân lên xuống. Các chức năng cần hai đối số cho điều đó. Đầu tiên là giá trị để làm tròn. Thứ hai là số chữ số sau dấu thập phân (
round_decimals_up(8.343)
# Returns: 8.35
8) để làm tròn đến (Lutz, 2013; Sweigart, 2015).

Vì vậy, để làm tròn đến 2 số thập phân chúng tôi làm:

round(12.54673, 2)
# Returns: 12.55

# Ví dụ: Các chữ số thập phân tròn lên xuống

Để xem cách thức hoạt động

Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
5 trong thực tế, hãy để khám phá chương trình nhỏ sau:

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)

Mã này đầu tiên làm cho năm biến dấu phẩy động. Mỗi số có một số số sau điểm thập phân (

round_decimals_up(8.343)
# Returns: 8.35
8). Chúng tôi lưu trữ các giá trị của chúng trong các biến
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
1 đến
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
2.

Sau đó, chúng tôi làm tròn mỗi nơi đến một số vị trí thập phân. Chúng tôi gọi hàm

Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
5 với hai đối số. Đầu tiên là biến chúng tôi vừa thực hiện. Số lượng số thập phân để làm tròn đến. Ở đây chúng tôi sử dụng các giá trị
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
4 đến
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
5 cho điều đó. Chúng tôi lưu trữ các giá trị tròn trong các biến mới (
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
6 đến
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
7).

Sau đó, chức năng Python từ

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
8 hiển thị cả giá trị ban đầu và tròn. Với phương thức chuỗi
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
9, chúng tôi sắp xếp giá trị ban đầu sang trái. Điều đó làm cho một đầu ra giống như bàn đẹp. Như chúng ta có thể thấy từ đầu ra, mỗi giá trị được làm tròn chính xác:

Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5

# Vòng thập phân ở Python

Một lựa chọn khác là luôn luôn làm tròn một số chữ số thập phân. Đó là ví dụ như những gì các công ty làm: thay vì giá tròn lên hoặc giảm, các giá trị như

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
0 thay vào đó luôn được làm tròn lên tới 4,55 đô la.

Python, tuy nhiên, không có chức năng tích hợp cho điều đó. Nhưng chúng ta có thể làm cho riêng mình và tận dụng chức năng

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
1. Khi chúng ta làm, sử dụng chức năng trông như thế này:

round_decimals_up(8.343)
# Returns: 8.35

Ở đây, cách chúng tôi mã hóa chức năng

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
2:

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor

Hàm này có hai tham số: giá trị thành vòng (

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
3) và có bao nhiêu vị trí thập phân cần có (
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
4). Khi tham số sau đó được đặt, nó mặc định là
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
5.

Mã chức năng có hai phần. Đầu tiên xác nhận và xử lý tham số

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
4. Thứ hai thực hiện làm tròn thực tế.

Đối với xác nhận đó, chúng tôi sử dụng câu lệnh của Python đã được đưa ra. Điều kiện đầu tiên có

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7 xem tham số
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
4 có phải là một cái gì đó khác hơn một số nguyên (
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
9). Khi có, chúng tôi tạo ra
Value:          Rounded:
3.14159265359   3.15
1845.7409947    1845.75
-100.95         -100.95
9.5432          9.55
34.49953        34.5
0 để thông báo cho người dùng rằng hàm được sử dụng sai cách.

Tiếp theo, chúng tôi kiểm tra xem

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
4 có nằm dưới (
Value:          Rounded:
3.14159265359   3.15
1845.7409947    1845.75
-100.95         -100.95
9.5432          9.55
34.49953        34.5
2) không. Vì hàm không biết cách xử lý các vị trí thập phân âm, chúng tôi kích hoạt
Value:          Rounded:
3.14159265359   3.15
1845.7409947    1845.75
-100.95         -100.95
9.5432          9.55
34.49953        34.5
3 để truyền đạt điều này với người dùng chức năng.

Thử nghiệm

Value:          Rounded:
3.14159265359   3.15
1845.7409947    1845.75
-100.95         -100.95
9.5432          9.55
34.49953        34.5
4 cuối cùng xem nếu tham số
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
4 bằng (
Value:          Rounded:
3.14159265359   3.15
1845.7409947    1845.75
-100.95         -100.95
9.5432          9.55
34.49953        34.5
6)
Value:          Rounded:
3.14159265359   3.15
1845.7409947    1845.75
-100.95         -100.95
9.5432          9.55
34.49953        34.5
7. Khi hàm phải làm tròn một giá trị mà không có vị trí thập phân, chúng ta chỉ có thể thực hiện hàm
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
1 và làm tròn đến một số toàn bộ.

Sau đó, chúng ta đến phần thứ hai của hàm. Đây là nơi chúng tôi làm tròn thực tế đến các vị trí thập phân. Nhưng trước tiên chúng tôi xác định hệ số hiệu chỉnh. Để có được giá trị đó, chúng tôi nâng

Value:          Rounded:
3.14159265359   3.15
1845.7409947    1845.75
-100.95         -100.95
9.5432          9.55
34.49953        34.5
9 lên sức mạnh của tham số
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
4. Chúng tôi lưu trữ kết quả trong biến
round_decimals_down(2.348, 2)
# Or:
round_decimals_down(2.348)
# Both return: 2.34
1.

Tiếp theo, chúng tôi gọi hàm

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
1. Bên trong dấu ngoặc đơn của nó, chúng tôi chỉ định giá trị để làm tròn (
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
3) nhân với
round_decimals_down(2.348, 2)
# Or:
round_decimals_down(2.348)
# Both return: 2.34
1. Sau đó, chúng tôi chia kết quả tròn với
round_decimals_down(2.348, 2)
# Or:
round_decimals_down(2.348)
# Both return: 2.34
1 để lấy lại số lượng thập phân thích hợp.

Giả sử chức năng nên tròn

round_decimals_down(2.348, 2)
# Or:
round_decimals_down(2.348)
# Both return: 2.34
6 đến 2 thập phân. Mã sau đó lần đầu tiên nhân
round_decimals_down(2.348, 2)
# Or:
round_decimals_down(2.348)
# Both return: 2.34
6 với
round_decimals_down(2.348, 2)
# Or:
round_decimals_down(2.348)
# Both return: 2.34
8 (102).
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
1 sau đó làm tròn
import math


def round_decimals_down(number:float, decimals:int=2):
    """
    Returns a value rounded down to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.floor(number)

    factor = 10 ** decimals
    return math.floor(number * factor) / factor
0 lên đến một số nguyên (
import math


def round_decimals_down(number:float, decimals:int=2):
    """
    Returns a value rounded down to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.floor(number)

    factor = 10 ** decimals
    return math.floor(number * factor) / factor
1). Cuối cùng, chúng tôi chia trở lại với
round_decimals_down(2.348, 2)
# Or:
round_decimals_down(2.348)
# Both return: 2.34
8 để có được kết quả thành hai chữ số thập phân:
import math


def round_decimals_down(number:float, decimals:int=2):
    """
    Returns a value rounded down to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.floor(number)

    factor = 10 ** decimals
    return math.floor(number * factor) / factor
3.

# Ví dụ: Làm tròn đến 2 chữ số thập phân

Để xem cách tương tác với chức năng

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
2 tùy chỉnh đó, hãy để sử dụng nó trong một chương trình nhỏ:

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)

Chương trình này lần đầu tiên nhập mô -đun

import math


def round_decimals_down(number:float, decimals:int=2):
    """
    Returns a value rounded down to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.floor(number)

    factor = 10 ** decimals
    return math.floor(number * factor) / factor
5. Điều đó làm cho chức năng
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
1 có sẵn cho chức năng tùy chỉnh của chúng tôi, chúng tôi sao chép/dán vào chương trình tiếp theo.

Sau đó, chúng tôi thực hiện năm biến (

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
1 đến
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
2). Mỗi người có một giá trị điểm nổi với một vài chữ số thập phân. Hãy để vòng tròn giá trị của họ.

Vì vậy, chúng tôi gọi hàm

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
2 nhiều lần. Chúng tôi cung cấp cho nó một đối số: giá trị thành vòng. Hàm sau đó tự động sử dụng hai vị trí thập phân để làm tròn. Chúng tôi đặt các giá trị tròn trong các biến
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
6 đến
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
7.

Hàm

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
8 sau đó xuất ra giá trị ban đầu và tròn. Đây là cách chức năng làm tròn mọi biến lên đến hai chữ số thập phân:

Value:          Rounded:
3.14159265359   3.15
1845.7409947    1845.75
-100.95         -100.95
9.5432          9.55
34.49953        34.5

# Vòng thập phân ở Python

Cách thứ ba để xử lý các chữ số thập phân là luôn luôn làm tròn. Đây là những gì một giáo viên nghiêm ngặt có thể sử dụng: thay vì vòng

import math


def round_decimals_down(number:float, decimals:int=2):
    """
    Returns a value rounded down to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.floor(number)

    factor = 10 ** decimals
    return math.floor(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to a certain number of decimal places
roundA = round_decimals_down(valueA, 5)
roundB = round_decimals_down(valueB, 4)
roundC = round_decimals_down(valueC, 3)
roundD = round_decimals_down(valueD, 2)
roundE = round_decimals_down(valueE, 1)

# Display the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
3 và
import math


def round_decimals_down(number:float, decimals:int=2):
    """
    Returns a value rounded down to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.floor(number)

    factor = 10 ** decimals
    return math.floor(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to a certain number of decimal places
roundA = round_decimals_down(valueA, 5)
roundB = round_decimals_down(valueB, 4)
roundC = round_decimals_down(valueC, 3)
roundD = round_decimals_down(valueD, 2)
roundE = round_decimals_down(valueE, 1)

# Display the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
4, thay vào đó anh ấy hoặc cô ấy tròn xuống
import math


def round_decimals_down(number:float, decimals:int=2):
    """
    Returns a value rounded down to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.floor(number)

    factor = 10 ** decimals
    return math.floor(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to a certain number of decimal places
roundA = round_decimals_down(valueA, 5)
roundB = round_decimals_down(valueB, 4)
roundC = round_decimals_down(valueC, 3)
roundD = round_decimals_down(valueD, 2)
roundE = round_decimals_down(valueE, 1)

# Display the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
5 và
import math


def round_decimals_down(number:float, decimals:int=2):
    """
    Returns a value rounded down to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.floor(number)

    factor = 10 ** decimals
    return math.floor(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to a certain number of decimal places
roundA = round_decimals_down(valueA, 5)
roundB = round_decimals_down(valueB, 4)
roundC = round_decimals_down(valueC, 3)
roundD = round_decimals_down(valueD, 2)
roundE = round_decimals_down(valueE, 1)

# Display the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
6.

Không có chức năng tích hợp trong Python cho loại làm tròn đó. Nhưng chúng ta có thể xây dựng của riêng mình và tận dụng chức năng

import math


def round_decimals_down(number:float, decimals:int=2):
    """
    Returns a value rounded down to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.floor(number)

    factor = 10 ** decimals
    return math.floor(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to a certain number of decimal places
roundA = round_decimals_down(valueA, 5)
roundB = round_decimals_down(valueB, 4)
roundC = round_decimals_down(valueC, 3)
roundD = round_decimals_down(valueD, 2)
roundE = round_decimals_down(valueE, 1)

# Display the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7. Sử dụng một chức năng tùy chỉnh như vậy trông giống như:

round_decimals_down(2.348, 2)
# Or:
round_decimals_down(2.348)
# Both return: 2.34

Điều đó hoạt động độc đáo. Bây giờ, hãy để mã mà chức năng tùy chỉnh đó:

import math


def round_decimals_down(number:float, decimals:int=2):
    """
    Returns a value rounded down to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.floor(number)

    factor = 10 ** decimals
    return math.floor(number * factor) / factor

Phần lớn chức năng này giống như hàm tùy chỉnh khác (

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
2) mà chúng ta đã thảo luận trước đó.

Sự khác biệt chính là bây giờ chúng tôi sử dụng hàm

import math


def round_decimals_down(number:float, decimals:int=2):
    """
    Returns a value rounded down to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.floor(number)

    factor = 10 ** decimals
    return math.floor(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to a certain number of decimal places
roundA = round_decimals_down(valueA, 5)
roundB = round_decimals_down(valueB, 4)
roundC = round_decimals_down(valueC, 3)
roundD = round_decimals_down(valueD, 2)
roundE = round_decimals_down(valueE, 1)

# Display the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7. Hàm này làm tròn tham số
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
3 nhân với biến
round_decimals_down(2.348, 2)
# Or:
round_decimals_down(2.348)
# Both return: 2.34
1 xuống. Sau đó, chúng tôi chia lại với
round_decimals_down(2.348, 2)
# Or:
round_decimals_down(2.348)
# Both return: 2.34
1 để có được số lượng thập phân phù hợp.

Giả sử chúng tôi yêu cầu chức năng tròn

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
03 xuống 2 chữ số thập phân. Mã sau đó nhân lên giá trị đó với
round_decimals_down(2.348, 2)
# Or:
round_decimals_down(2.348)
# Both return: 2.34
8.
import math


def round_decimals_down(number:float, decimals:int=2):
    """
    Returns a value rounded down to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.floor(number)

    factor = 10 ** decimals
    return math.floor(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to a certain number of decimal places
roundA = round_decimals_down(valueA, 5)
roundB = round_decimals_down(valueB, 4)
roundC = round_decimals_down(valueC, 3)
roundD = round_decimals_down(valueD, 2)
roundE = round_decimals_down(valueE, 1)

# Display the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7 sau đó làm tròn
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
06 xuống một số toàn bộ (
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
07). Khi chúng tôi chia trở lại với
round_decimals_down(2.348, 2)
# Or:
round_decimals_down(2.348)
# Both return: 2.34
8, chúng tôi sẽ nhận được số lượng số thập phân thích hợp:
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
09.

# Ví dụ: Làm tròn xuống 2 chữ số thập phân

Hãy để xem làm thế nào mà chức năng trên hoạt động trong thực tế. Chương trình nhỏ này có chức năng làm tròn 5 giá trị khác nhau:

import math


def round_decimals_down(number:float, decimals:int=2):
    """
    Returns a value rounded down to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.floor(number)

    factor = 10 ** decimals
    return math.floor(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to a certain number of decimal places
roundA = round_decimals_down(valueA, 5)
roundB = round_decimals_down(valueB, 4)
roundC = round_decimals_down(valueC, 3)
roundD = round_decimals_down(valueD, 2)
roundE = round_decimals_down(valueE, 1)

# Display the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)

Đầu tiên chúng tôi nhập mô -đun

import math


def round_decimals_down(number:float, decimals:int=2):
    """
    Returns a value rounded down to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.floor(number)

    factor = 10 ** decimals
    return math.floor(number * factor) / factor
5. Bằng cách đó, hàm
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
11 tùy chỉnh có thể sử dụng
import math


def round_decimals_down(number:float, decimals:int=2):
    """
    Returns a value rounded down to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.floor(number)

    factor = 10 ** decimals
    return math.floor(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to a certain number of decimal places
roundA = round_decimals_down(valueA, 5)
roundB = round_decimals_down(valueB, 4)
roundC = round_decimals_down(valueC, 3)
roundD = round_decimals_down(valueD, 2)
roundE = round_decimals_down(valueE, 1)

# Display the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7. Sau đó chúng tôi chèn chức năng tùy chỉnh đó.

Sau đó, chúng tôi thực hiện năm biến (

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
1 đến
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
2). Mỗi người giữ một giá trị với số thập phân để làm tròn xuống. Đó là những gì chúng tôi làm tiếp theo.

Vì vậy, chúng tôi liên tục thực hiện

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
11. Mỗi lần chúng tôi đưa ra hàm hai đối số: giá trị thành vòng và số chữ số thập phân để giữ. Chúng tôi đặt kết quả trong các biến mới (
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
6 đến
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
7).

Bit cuối cùng của mã đầu ra kết quả với hàm

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
8. Chúng tôi đã đưa ra giá trị ban đầu bằng phương pháp
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
9 và cũng bao gồm giá trị tròn. Khi đầu ra hiển thị, mỗi giá trị được làm tròn xuống một số vị trí thập phân nhất định:

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
0

# Vòng thập phân của danh sách và mảng Python

Các ví dụ trên luôn làm tròn một giá trị tại một thời điểm. Tuy nhiên, đôi khi, chúng tôi có một chuỗi với các giá trị mà chúng tôi muốn làm tròn đến một số chữ số thập phân. Hãy cùng xem cách làm như vậy trong Python.

# Danh sách các vòng đến các vị trí thập phân với danh sách hiểu biết

Một tùy chọn là làm tròn mọi giá trị với sự hiểu biết danh sách. Điều này chỉ cần một chút mã và chạy hiệu quả. Một danh sách hiểu hoạt động tốt nhất khi mã của bạn không phức tạp. Đơn giản chỉ cần làm tròn các giá trị đáp ứng dấu hiệu đó.

Ở đây, một ví dụ nhanh chóng:

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
1

Danh sách

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
20 mà chúng tôi thực hiện ở đây nhận được các giá trị của nó từ một danh sách hiểu. Sự hiểu biết đó thực thi biểu thức
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
21 cho mọi giá trị được tạo bởi vòng lặp
Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
6:
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
23. Đó là cách mà chúng ta đi qua
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
24 Liệt kê một yếu tố tại một thời điểm.

Ở đây, một chương trình nhỏ làm tròn mọi giá trị trong danh sách:

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
2

Trước tiên chúng tôi nhập mô -đun

import math


def round_decimals_down(number:float, decimals:int=2):
    """
    Returns a value rounded down to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.floor(number)

    factor = 10 ** decimals
    return math.floor(number * factor) / factor
5. Sau đó, chúng tôi chèn các hàm
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
11 và
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
2 mà chúng tôi đã thực hiện trước đó trong bài viết này.

Tiếp theo chúng tôi lập một danh sách với các con số. Danh sách

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
24 đó có 6 giá trị điểm nổi. Sau đó, chúng tôi làm tròn từng giá trị trong danh sách đó. Chúng tôi sử dụng ba toàn bộ danh sách khác nhau cho điều đó.

Các vòng đầu tiên mọi giá trị danh sách đến hai vị trí thập phân (lên và xuống) với hàm

Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
5. Chúng tôi đặt các giá trị tròn đó vào một danh sách mới,
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
30.

Độ hiểu hiểu danh sách thứ hai có hàm

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
2 làm tròn đến hai chữ số thập phân. Danh sách mới mà lợi nhuận này là những gì chúng tôi lưu trữ trong
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
32. Các vòng hiểu danh sách thứ ba và cuối cùng xuống đến hai vị trí thập phân (
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
11). Kết quả của sự hiểu biết đó được lưu trữ trong danh sách
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
34.

Chương trình Bit Bit của mã đầu ra cuối cùng tất cả bốn danh sách với chức năng

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
8. Đây là cách các giá trị tròn đó trông như thế nào:

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
3

Trong ví dụ trên, chúng tôi đã thực hiện các danh sách mới với danh sách hiểu. Nếu bạn không cần phải giữ các giá trị danh sách ban đầu, thì việc hiểu danh sách cũng có thể ghi đè lên danh sách hiện có. Để làm như vậy, chúng tôi đặt giá trị danh sách thành kết của kết quả của danh sách hiểu:

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
4

# Danh sách tròn đến các vị trí thập phân với vòng lặp Python

Cách thứ hai để các giá trị danh sách tròn là với vòng lặp

Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
6. Điều này đòi hỏi nhiều mã hơn một chút so với việc hiểu danh sách, nhưng có nhiều khả năng hơn và dễ đọc hơn trong các tình huống phức tạp.

Như một ví dụ nhanh:

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
5

Danh sách

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
38 chúng tôi thực hiện ở đây bắt đầu trống (
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
39). Nhưng bên trong vòng lặp
Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
6, chúng tôi điền vào danh sách đó bằng các giá trị. Vì vậy, chúng tôi gọi phương thức
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
41 trong danh sách và chuyển trong giá trị tròn từ danh sách ban đầu (
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
21).

Chương trình nhỏ này làm tròn danh sách các giá trị với

Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
6:

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
6

Trước tiên chúng tôi nhập mô -đun

import math


def round_decimals_down(number:float, decimals:int=2):
    """
    Returns a value rounded down to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.floor(number)

    factor = 10 ** decimals
    return math.floor(number * factor) / factor
5. Sau đó, chúng tôi sao chép/dán các hàm
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
11 và
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
2 mà chúng tôi đã thực hiện trước đó trong bài viết này.

Sau đó, chúng tôi lập một danh sách với một số giá trị dấu phẩy động, cả tích cực và tiêu cực. Sau đó, chúng tôi mã hóa ba danh sách trống (

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
39):
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
30,
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
32 và
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
34. Chúng sẽ giữ các giá trị tròn.

Để thực hiện điều đó, chúng tôi tạo ra một vòng lặp

Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
6. Vòng lặp này lặp qua mọi giá trị trong danh sách
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
24. Trong mỗi chu kỳ vòng lặp, giá trị danh sách hiện tại được truy cập với biến vòng lặp
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
53. Bên trong vòng lặp đó, chúng tôi nối một giá trị tròn vào mỗi trong ba danh sách với phương thức Python từ
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
41.

Chương trình Bit Bit của mã cuối cùng sau đó xuất ra danh sách ban đầu và các dẫn xuất tròn của nó. Ở đây, những gì hiển thị:

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7

Không cần các giá trị danh sách ban đầu? Trong trường hợp đó, bạn không phải lập một danh sách mới, riêng biệt. Thay vào đó, bạn có thể ghi đè lên một nơi hiện có. Một chức năng hữu ích cho điều đó là Python từ

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
55. Ví dụ:

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
8

# Mảng Python tròn đến 2 chữ số thập phân

Tất nhiên chúng ta cũng có thể làm tròn các bộ sưu tập khác hơn danh sách. Ở đây, cách chúng ta làm tròn mọi giá trị trong một mảng Python đến 2 số thập phân:

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
9

Đầu tiên chúng tôi nhập hàm tạo

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
56 từ mô -đun
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
57. Sau đó, chúng tôi tạo một mảng với các giá trị dấu phẩy động. Chúng tôi đặt tên cho mảng đó
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
24.

Sau đó, chúng tôi xây dựng một mảng thứ hai. Cái này có các giá trị từ mảng ban đầu được làm tròn đến 2 thập phân. Để thực hiện điều đó, chúng tôi sử dụng một danh sách hiểu. Đó là làm tròn từng giá trị thành hai chữ số thập phân (

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
21). Chúng tôi đặt tên cho mảng mới, tròn
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
20.

Hàm

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
8 sau đó xuất ra mảng gốc và tròn. Như chúng ta có thể nói, mỗi giá trị được làm tròn đúng đến hai vị trí thập phân:

Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
0

Bạn có cần phải giữ các giá trị mảng ban đầu không? Trong trường hợp đó, bạn có thể ghi đè lên mảng hiện có với các giá trị tròn của nó. Đây là cách: làm thế nào:

Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
1

# Vòng numpy mảng đến hai chữ số thập phân

Tất nhiên chúng ta cũng có thể làm tròn các giá trị của các mảng khác. Một ví dụ là những người từ gói lập trình số numpy cho Python. Để làm tròn một mảng numpy đến hai vị trí thập phân chúng tôi làm:

Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
2

Trước tiên chúng tôi nhập mô -đun

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
62. Sau đó, chúng tôi tạo ra một mảng numpy với hàm
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
63. Mảng đó có một số giá trị dấu phẩy động.

Sau đó, chúng tôi làm tròn mảng ban đầu đến hai chữ số thập phân. Vì vậy, chúng tôi gọi hàm

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
64 với hai đối số. Đầu tiên là mảng có giá trị đến tròn. Thứ hai là số lượng vị trí thập phân để làm tròn đến. Chúng tôi lưu trữ mảng hàm trả về trong biến
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
20.

Bit cuối cùng của mã đầu ra cả mảng gốc và tròn. Điều đó cho thấy cách các giá trị được làm tròn độc đáo đến 2 thập phân:

Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
3

Hàm

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
64 làm tròn lên xuống. Để luôn làm tròn hoặc luôn làm tròn xuống, chúng tôi phải sử dụng các hàm
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
2 và
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
11 tùy chỉnh.

Khi chúng tôi sử dụng các hàm đó trong một danh sách hiểu, chúng có thể làm tròn từng giá trị từ một mảng numpy. Ví dụ:

Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
4

ĐỌC THÊM

  • Vòng python giá trị cho một số toàn bộ.
  • Cắt ngắn giá trị python thành một số
  • Cắt ngắn đến một số vị trí thập phân nhất định trong Python

# Bản tóm tắt

Có ba cách để số tròn đến một số vị trí thập phân nhất định. Để làm tròn và xuống, chúng tôi sử dụng chức năng Python từ ____25. Đối số đầu tiên chúng tôi đưa ra chức năng đó là số đến tròn. Đối số thứ hai số lượng vị trí thập phân để làm tròn đến.

Python không có chức năng luôn làm tròn các chữ số thập phân (

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
70 thành
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
71). Nhưng khi chúng tôi tận dụng chức năng
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
1 trong một chức năng tùy chỉnh, chúng tôi có thể dễ dàng thực hiện điều đó.

Không có chức năng nào luôn làm tròn các chữ số thập phân (

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
73 trở thành
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
74). Nhưng khi chúng ta bọc hàm
import math


def round_decimals_down(number:float, decimals:int=2):
    """
    Returns a value rounded down to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.floor(number)

    factor = 10 ** decimals
    return math.floor(number * factor) / factor


# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to a certain number of decimal places
roundA = round_decimals_down(valueA, 5)
roundB = round_decimals_down(valueB, 4)
roundC = round_decimals_down(valueC, 3)
roundD = round_decimals_down(valueD, 2)
roundE = round_decimals_down(valueE, 1)

# Display the results
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7 trong một hàm tùy chỉnh, chúng ta có thể tự tạo hành vi đó.

Để làm tròn mọi giá trị trong danh sách hoặc mảng, chúng ta có thể sử dụng vòng lặp hiểu biết hoặc vòng lặp

Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
6. Đầu tiên là nhỏ gọn và lần thứ hai dễ làm việc hơn.

Người giới thiệu

Lutz, M. (2013). Học Python (Phiên bản thứ 5). Sebastopol, CA: O hèReilly Media.

Sweigart, A. (2015). Tự động hóa những thứ nhàm chán với Python: Lập trình thực tế cho toàn bộ người mới bắt đầu. San Francisco, CA: Không có báo chí tinh bột.

Python.org (N.D.). Chức năng tích hợp sẵn. Truy cập vào ngày 8 tháng 11 năm 2019, từ https://docs.python.org/3/l Library/fives.html

Cập nhật lần cuối vào ngày 9 tháng 9 năm 2020 (xuất bản ngày 20 tháng 12 năm 2019).

«Tất cả các bài báo Python

Làm thế nào để bạn làm tròn các vị trí thập phân ở Python?

Python chức năng vòng () tích hợp của Python có hàm vòng () tích hợp có hai đối số số, N và NDigits và trả về số N được làm tròn cho ndigits. Đối số NDigits mặc định là 0, do đó, để lại kết quả trong một số được làm tròn cho một số nguyên.round() Function Python has a built-in round() function that takes two numeric arguments, n and ndigits , and returns the number n rounded to ndigits . The ndigits argument defaults to zero, so leaving it out results in a number rounded to an integer.

Python có vòng 0,5 lên hay xuống không?

Trong Python, chức năng vòng () làm tròn lên hay xuống?Hàm vòng () có thể làm tròn các giá trị lên và xuống cả hai tùy thuộc vào tình huống.Đối với 0,5, nó làm tròn lên.For = 0,5, hàm vòng () làm tròn số đến số chẵn gần nhất.For <0.5, it rounds down, and for >0.5, it rounds up. For =0.5, the round() function rounds the number off to the nearest even number.

Làm cách nào để làm tròn 0,5 trong Python?

Vòng trôi nổi lên đến 0,5 gần nhất trong Python # Gọi phương thức Math.ceil () vượt qua số lượng nhân với 2.Chia kết quả cho 2.Kết quả của phép tính là số được làm tròn đến 0,5 gần nhất.Call the math. ceil() method passing it the number multiplied by 2 . Divide the result by 2 . The result of the calculation is the number rounded up to the nearest 0.5 .

Làm thế nào để bạn sử dụng .2f trong Python?

2F là một trình giữ chỗ cho số điểm nổi.Vì vậy, %D được thay thế bằng giá trị đầu tiên của tuple tức là 12 và %.2F được thay thế bằng giá trị thứ hai I.E 150.87612.... Định dạng chuỗi Python ..