Hướng dẫn python add percentage to number - python thêm phần trăm vào số

Câu trả lời của Brian (một chức năng tùy chỉnh) là điều chính xác và đơn giản nhất để làm nói chung.

Nhưng nếu bạn thực sự muốn xác định một loại số với toán tử (không chuẩn) ' %', như máy tính bàn làm, để 'x % y' có nghĩa là x * y / 100.0, thì từ Python 2.6 trở đi, bạn có thể xác định lại toán tử mod ():mod() operator:

import numbers

class MyNumberClasswithPct(numbers.Real):
    def __mod__(self,other):
        """Override the builtin % to give X * Y / 100.0 """
        return (self * other)/ 100.0
    # Gotta define the other 21 numeric methods...
    def __mul__(self,other):
        return self * other # ... which should invoke other.__rmul__(self)
    #...

Điều này có thể nguy hiểm nếu bạn sử dụng toán tử '%' trên hỗn hợp MynumberClassWithPCT với các số nguyên hoặc phao thông thường.

Điều gì cũng tẻ nhạt về mã này là bạn cũng phải xác định tất cả 21 phương pháp khác của một tích phân hoặc thực, để tránh kiểu loại khó chịu và khó hiểu sau đây khi bạn khởi tạo nó

("Can't instantiate abstract class MyNumberClasswithPct with abstract methods __abs__,  __add__, __div__, __eq__, __float__, __floordiv__, __le__, __lt__, __mul__,  __neg__, __pos__, __pow__, __radd__, __rdiv__, __rfloordiv__, __rmod__, __rmul__,  __rpow__, __rtruediv__, __truediv__, __trunc__")

Tính tỷ lệ phần trăm trong Python #

Để tính tỷ lệ phần trăm trong Python:

  1. Sử dụng toán tử phân chia
    ("Can't instantiate abstract class MyNumberClasswithPct with abstract methods __abs__,  __add__, __div__, __eq__, __float__, __floordiv__, __le__, __lt__, __mul__,  __neg__, __pos__, __pow__, __radd__, __rdiv__, __rfloordiv__, __rmod__, __rmul__,  __rpow__, __rtruediv__, __truediv__, __trunc__")
    
    1 để chia một số cho một số khác.
  2. Nhân số chỉ số với
    ("Can't instantiate abstract class MyNumberClasswithPct with abstract methods __abs__,  __add__, __div__, __eq__, __float__, __floordiv__, __le__, __lt__, __mul__,  __neg__, __pos__, __pow__, __radd__, __rdiv__, __rfloordiv__, __rmod__, __rmul__,  __rpow__, __rtruediv__, __truediv__, __trunc__")
    
    2 để có được tỷ lệ phần trăm.
  3. Kết quả cho thấy bao nhiêu phần trăm số đầu tiên của số thứ hai.

Copied!

def is_what_percent_of(num_a, num_b): return (num_a / num_b) * 100 print(is_what_percent_of(25, 75)) # 👉️ 33.33 print(is_what_percent_of(15, 93)) # 👉️ 16.12903.. print(round(is_what_percent_of(15, 93), 2)) # 👉️ 16.13 # -------------------------------------------------- def get_percentage_increase(num_a, num_b): return ((num_a - num_b) / num_b) * 100 print(get_percentage_increase(60, 30)) # 👉️ 100.0 print(get_percentage_increase(40, 100)) # 👉️ -60.0 # -------------------------------------------------- def get_remainder(num_a, num_b): return num_a % num_b print(get_remainder(50, 15)) # 👉️ 5 print(get_remainder(50, 20)) # 👉️ 10

Hàm đầu tiên mất 2 số và trả về phần trăm số đầu tiên của số thứ hai.

Ví dụ,

("Can't instantiate abstract class MyNumberClasswithPct with abstract methods __abs__,  __add__, __div__, __eq__, __float__, __floordiv__, __le__, __lt__, __mul__,  __neg__, __pos__, __pow__, __radd__, __rdiv__, __rfloordiv__, __rmod__, __rmul__,  __rpow__, __rtruediv__, __truediv__, __trunc__")
3 cho thấy
("Can't instantiate abstract class MyNumberClasswithPct with abstract methods __abs__,  __add__, __div__, __eq__, __float__, __floordiv__, __le__, __lt__, __mul__,  __neg__, __pos__, __pow__, __radd__, __rdiv__, __rfloordiv__, __rmod__, __rmul__,  __rpow__, __rtruediv__, __truediv__, __trunc__")
4 là
("Can't instantiate abstract class MyNumberClasswithPct with abstract methods __abs__,  __add__, __div__, __eq__, __float__, __floordiv__, __le__, __lt__, __mul__,  __neg__, __pos__, __pow__, __radd__, __rdiv__, __rfloordiv__, __rmod__, __rmul__,  __rpow__, __rtruediv__, __truediv__, __trunc__")
5 của
("Can't instantiate abstract class MyNumberClasswithPct with abstract methods __abs__,  __add__, __div__, __eq__, __float__, __floordiv__, __le__, __lt__, __mul__,  __neg__, __pos__, __pow__, __radd__, __rdiv__, __rfloordiv__, __rmod__, __rmul__,  __rpow__, __rtruediv__, __truediv__, __trunc__")
6.

Copied!

print((25 / 50) * 100) # 👉️ 50.0

Khi tính toán tỷ lệ phần trăm, bạn có thể cần làm tròn đến một số chữ số cụ thể sau thập phân.

Hàm vòng lấy 2 tham số sau:

TênSự mô tả
("Can't instantiate abstract class MyNumberClasswithPct with abstract methods __abs__,  __add__, __div__, __eq__, __float__, __floordiv__, __le__, __lt__, __mul__,  __neg__, __pos__, __pow__, __radd__, __rdiv__, __rfloordiv__, __rmod__, __rmul__,  __rpow__, __rtruediv__, __truediv__, __trunc__")
7
số để làm tròn đến độ chính xác
("Can't instantiate abstract class MyNumberClasswithPct with abstract methods __abs__,  __add__, __div__, __eq__, __float__, __floordiv__, __le__, __lt__, __mul__,  __neg__, __pos__, __pow__, __radd__, __rdiv__, __rfloordiv__, __rmod__, __rmul__,  __rpow__, __rtruediv__, __truediv__, __trunc__")
8 sau thập phân
("Can't instantiate abstract class MyNumberClasswithPct with abstract methods __abs__,  __add__, __div__, __eq__, __float__, __floordiv__, __le__, __lt__, __mul__,  __neg__, __pos__, __pow__, __radd__, __rdiv__, __rfloordiv__, __rmod__, __rmul__,  __rpow__, __rtruediv__, __truediv__, __trunc__")
8
Số chữ số sau số thập phân Số lượng nên có sau khi hoạt động (tùy chọn)

Copied!

print(round((33 / 65) * 100, 2)) # 👉️ 50.77

Hàm

Copied!

def is_what_percent_of(num_a, num_b): return (num_a / num_b) * 100 print(is_what_percent_of(25, 75)) # 👉️ 33.33 print(is_what_percent_of(15, 93)) # 👉️ 16.12903.. print(round(is_what_percent_of(15, 93), 2)) # 👉️ 16.13 # -------------------------------------------------- def get_percentage_increase(num_a, num_b): return ((num_a - num_b) / num_b) * 100 print(get_percentage_increase(60, 30)) # 👉️ 100.0 print(get_percentage_increase(40, 100)) # 👉️ -60.0 # -------------------------------------------------- def get_remainder(num_a, num_b): return num_a % num_b print(get_remainder(50, 15)) # 👉️ 5 print(get_remainder(50, 20)) # 👉️ 10
0 trả về số được làm tròn đến độ chính xác của
("Can't instantiate abstract class MyNumberClasswithPct with abstract methods __abs__,  __add__, __div__, __eq__, __float__, __floordiv__, __le__, __lt__, __mul__,  __neg__, __pos__, __pow__, __radd__, __rdiv__, __rfloordiv__, __rmod__, __rmul__,  __rpow__, __rtruediv__, __truediv__, __trunc__")
8 sau điểm thập phân.

Nếu

("Can't instantiate abstract class MyNumberClasswithPct with abstract methods __abs__,  __add__, __div__, __eq__, __float__, __floordiv__, __le__, __lt__, __mul__,  __neg__, __pos__, __pow__, __radd__, __rdiv__, __rfloordiv__, __rmod__, __rmul__,  __rpow__, __rtruediv__, __truediv__, __trunc__")
8 bị bỏ qua, hàm trả về số nguyên gần nhất.

Lưu ý rằng nếu bạn cố gắng chia cho

Copied!

def is_what_percent_of(num_a, num_b): return (num_a / num_b) * 100 print(is_what_percent_of(25, 75)) # 👉️ 33.33 print(is_what_percent_of(15, 93)) # 👉️ 16.12903.. print(round(is_what_percent_of(15, 93), 2)) # 👉️ 16.13 # -------------------------------------------------- def get_percentage_increase(num_a, num_b): return ((num_a - num_b) / num_b) * 100 print(get_percentage_increase(60, 30)) # 👉️ 100.0 print(get_percentage_increase(40, 100)) # 👉️ -60.0 # -------------------------------------------------- def get_remainder(num_a, num_b): return num_a % num_b print(get_remainder(50, 15)) # 👉️ 5 print(get_remainder(50, 20)) # 👉️ 10
3, bạn sẽ nhận được

Copied!

def is_what_percent_of(num_a, num_b): return (num_a / num_b) * 100 print(is_what_percent_of(25, 75)) # 👉️ 33.33 print(is_what_percent_of(15, 93)) # 👉️ 16.12903.. print(round(is_what_percent_of(15, 93), 2)) # 👉️ 16.13 # -------------------------------------------------- def get_percentage_increase(num_a, num_b): return ((num_a - num_b) / num_b) * 100 print(get_percentage_increase(60, 30)) # 👉️ 100.0 print(get_percentage_increase(40, 100)) # 👉️ -60.0 # -------------------------------------------------- def get_remainder(num_a, num_b): return num_a % num_b print(get_remainder(50, 15)) # 👉️ 5 print(get_remainder(50, 20)) # 👉️ 10
4.

Nếu bạn cần xử lý việc này theo bất kỳ cách nào, hãy sử dụng khối

Copied!

def is_what_percent_of(num_a, num_b): return (num_a / num_b) * 100 print(is_what_percent_of(25, 75)) # 👉️ 33.33 print(is_what_percent_of(15, 93)) # 👉️ 16.12903.. print(round(is_what_percent_of(15, 93), 2)) # 👉️ 16.13 # -------------------------------------------------- def get_percentage_increase(num_a, num_b): return ((num_a - num_b) / num_b) * 100 print(get_percentage_increase(60, 30)) # 👉️ 100.0 print(get_percentage_increase(40, 100)) # 👉️ -60.0 # -------------------------------------------------- def get_remainder(num_a, num_b): return num_a % num_b print(get_remainder(50, 15)) # 👉️ 5 print(get_remainder(50, 20)) # 👉️ 10
5 để xử lý lỗi.

Copied!

def is_what_percent_of(num_a, num_b): try: return (num_a / num_b) * 100 except ZeroDivisionError: return 0 print(is_what_percent_of(25, 0)) # 👉️ 0

Hàm thứ hai cho thấy làm thế nào để có được phần trăm tăng / giảm giữa hai số.

Copied!

def get_percentage_increase(num_a, num_b): return ((num_a - num_b) / num_b) * 100 print(get_percentage_increase(60, 30)) # 👉️ 100.0 print(get_percentage_increase(40, 100)) # 👉️ -60.0

Ví dụ đầu tiên cho thấy tỷ lệ phần trăm tăng từ

Copied!

def is_what_percent_of(num_a, num_b): return (num_a / num_b) * 100 print(is_what_percent_of(25, 75)) # 👉️ 33.33 print(is_what_percent_of(15, 93)) # 👉️ 16.12903.. print(round(is_what_percent_of(15, 93), 2)) # 👉️ 16.13 # -------------------------------------------------- def get_percentage_increase(num_a, num_b): return ((num_a - num_b) / num_b) * 100 print(get_percentage_increase(60, 30)) # 👉️ 100.0 print(get_percentage_increase(40, 100)) # 👉️ -60.0 # -------------------------------------------------- def get_remainder(num_a, num_b): return num_a % num_b print(get_remainder(50, 15)) # 👉️ 5 print(get_remainder(50, 20)) # 👉️ 10
6 lên

Copied!

def is_what_percent_of(num_a, num_b): return (num_a / num_b) * 100 print(is_what_percent_of(25, 75)) # 👉️ 33.33 print(is_what_percent_of(15, 93)) # 👉️ 16.12903.. print(round(is_what_percent_of(15, 93), 2)) # 👉️ 16.13 # -------------------------------------------------- def get_percentage_increase(num_a, num_b): return ((num_a - num_b) / num_b) * 100 print(get_percentage_increase(60, 30)) # 👉️ 100.0 print(get_percentage_increase(40, 100)) # 👉️ -60.0 # -------------------------------------------------- def get_remainder(num_a, num_b): return num_a % num_b print(get_remainder(50, 15)) # 👉️ 5 print(get_remainder(50, 20)) # 👉️ 10
7 là

Copied!

def is_what_percent_of(num_a, num_b): return (num_a / num_b) * 100 print(is_what_percent_of(25, 75)) # 👉️ 33.33 print(is_what_percent_of(15, 93)) # 👉️ 16.12903.. print(round(is_what_percent_of(15, 93), 2)) # 👉️ 16.13 # -------------------------------------------------- def get_percentage_increase(num_a, num_b): return ((num_a - num_b) / num_b) * 100 print(get_percentage_increase(60, 30)) # 👉️ 100.0 print(get_percentage_increase(40, 100)) # 👉️ -60.0 # -------------------------------------------------- def get_remainder(num_a, num_b): return num_a % num_b print(get_remainder(50, 15)) # 👉️ 5 print(get_remainder(50, 20)) # 👉️ 10
8.

Và ví dụ thứ hai cho thấy tỷ lệ phần trăm tăng từ

Copied!

def is_what_percent_of(num_a, num_b): return (num_a / num_b) * 100 print(is_what_percent_of(25, 75)) # 👉️ 33.33 print(is_what_percent_of(15, 93)) # 👉️ 16.12903.. print(round(is_what_percent_of(15, 93), 2)) # 👉️ 16.13 # -------------------------------------------------- def get_percentage_increase(num_a, num_b): return ((num_a - num_b) / num_b) * 100 print(get_percentage_increase(60, 30)) # 👉️ 100.0 print(get_percentage_increase(40, 100)) # 👉️ -60.0 # -------------------------------------------------- def get_remainder(num_a, num_b): return num_a % num_b print(get_remainder(50, 15)) # 👉️ 5 print(get_remainder(50, 20)) # 👉️ 10
9 lên
("Can't instantiate abstract class MyNumberClasswithPct with abstract methods __abs__,  __add__, __div__, __eq__, __float__, __floordiv__, __le__, __lt__, __mul__,  __neg__, __pos__, __pow__, __radd__, __rdiv__, __rfloordiv__, __rmod__, __rmul__,  __rpow__, __rtruediv__, __truediv__, __trunc__")
2 là

Copied!

print((25 / 50) * 100) # 👉️ 50.0
1.

Nếu bạn luôn cần có một số dương, hãy sử dụng hàm

Copied!

print((25 / 50) * 100) # 👉️ 50.0
2.

Copied!

def get_percentage_increase(num_a, num_b): return abs((num_a - num_b) / num_b) * 100 print(get_percentage_increase(60, 30)) # 👉️ 100.0 print(get_percentage_increase(40, 100)) # 👉️ 60.0

Hàm ABS trả về giá trị tuyệt đối của một số. Nói cách khác, nếu số là dương, số được trả về và nếu số là âm, thì phủ định của số được trả về.

Bằng cách này, chúng tôi luôn được đảm bảo để có được một số dương khi tính toán sự khác biệt về tỷ lệ phần trăm giữa hai số.

Bạn cũng có thể cần xử lý bộ phận theo trường hợp bằng không.

Copied!

def get_percentage_increase(num_a, num_b): try: return abs((num_a - num_b) / num_b) * 100 except ZeroDivisionError: return float('inf') print(get_percentage_increase(60, 0)) # 👉️ inf print(get_percentage_increase(60, 60)) # 👉️ 0.0 print(get_percentage_increase(60, 120)) # 👉️ 50.0

Nếu chúng tôi gặp lỗi

Copied!

def is_what_percent_of(num_a, num_b): return (num_a / num_b) * 100 print(is_what_percent_of(25, 75)) # 👉️ 33.33 print(is_what_percent_of(15, 93)) # 👉️ 16.12903.. print(round(is_what_percent_of(15, 93), 2)) # 👉️ 16.13 # -------------------------------------------------- def get_percentage_increase(num_a, num_b): return ((num_a - num_b) / num_b) * 100 print(get_percentage_increase(60, 30)) # 👉️ 100.0 print(get_percentage_increase(40, 100)) # 👉️ -60.0 # -------------------------------------------------- def get_remainder(num_a, num_b): return num_a % num_b print(get_remainder(50, 15)) # 👉️ 5 print(get_remainder(50, 20)) # 👉️ 10
4, chúng tôi sẽ trả về Infinity, tuy nhiên bạn có thể xử lý lỗi theo bất kỳ cách nào khác phù hợp với trường hợp sử dụng của bạn.

Hàm thứ ba trong mẫu mã sử dụng toán tử modulo

Copied!

print((25 / 50) * 100) # 👉️ 50.0
4.

Copied!

def get_remainder(num_a, num_b): return num_a % num_b print(get_remainder(50, 15)) # 👉️ 5 print(get_remainder(50, 20)) # 👉️ 10

Toán tử modulo (%) trả về phần còn lại từ sự phân chia giá trị thứ nhất cho phần thứ hai.

("Can't instantiate abstract class MyNumberClasswithPct with abstract methods __abs__,  __add__, __div__, __eq__, __float__, __floordiv__, __le__, __lt__, __mul__,  __neg__, __pos__, __pow__, __radd__, __rdiv__, __rfloordiv__, __rmod__, __rmul__,  __rpow__, __rtruediv__, __truediv__, __trunc__")
0

Nếu giá trị ở phía bên phải bằng không, toán tử sẽ tăng ngoại lệ

Copied!

def is_what_percent_of(num_a, num_b): return (num_a / num_b) * 100 print(is_what_percent_of(25, 75)) # 👉️ 33.33 print(is_what_percent_of(15, 93)) # 👉️ 16.12903.. print(round(is_what_percent_of(15, 93), 2)) # 👉️ 16.13 # -------------------------------------------------- def get_percentage_increase(num_a, num_b): return ((num_a - num_b) / num_b) * 100 print(get_percentage_increase(60, 30)) # 👉️ 100.0 print(get_percentage_increase(40, 100)) # 👉️ -60.0 # -------------------------------------------------- def get_remainder(num_a, num_b): return num_a % num_b print(get_remainder(50, 15)) # 👉️ 5 print(get_remainder(50, 20)) # 👉️ 10
4.

Các giá trị bên trái và bên phải cũng có thể là số điểm nổi.

Làm thế nào tôi có thể thêm tỷ lệ phần trăm vào một số?

Làm cách nào để thêm tỷ lệ tăng tỷ lệ phần trăm vào một số ?..
Chia số bạn muốn tăng thêm 100 để tìm 1% của nó ..
Nhân 1% với tỷ lệ phần trăm đã chọn của bạn ..
Thêm số này vào số ban đầu của bạn ..
Ở đó bạn đi, bạn vừa thêm một phần trăm tăng lên một số !.

Làm thế nào để bạn viết tỷ lệ phần trăm bằng số trong Python?

Sử dụng str.format () với "{: .0%}" làm str để định dạng số theo tỷ lệ phần trăm.Để bao gồm một số vị trí số thập phân cụ thể, hãy sử dụng "{:. N%}", trong đó n là số lượng số thập phân mong muốn. format() with "{:. 0%}" as str to format the number as a percentage. To include a specific number of decimal places, use "{:. n%}" , where n is the desired number of decimal places.

Làm thế nào để bạn thêm 20% vào một giá trị?

Nhân giá ban đầu với 0,2 để tìm số lượng đánh dấu 20 phần trăm hoặc nhân nó với 1,2 để tìm tổng giá (bao gồm cả đánh dấu).Nếu bạn có giá cuối cùng (bao gồm cả đánh dấu) và muốn biết giá ban đầu là bao nhiêu, chia cho 1,2. to find the amount of a 20 percent markup, or multiply it by 1.2 to find the total price (including markup). If you have the final price (including markup) and want to know what the original price was, divide by 1.2.

Làm thế nào để bạn tính toán phần trăm thay đổi trong Python?

Hàm pct_change () tính toán tỷ lệ phần trăm thay đổi giữa phần tử hiện tại và phần tử trước.Hàm này theo mặc định tính toán tỷ lệ phần trăm thay đổi từ hàng trước đó ngay lập tức. calculates the percentage change between the current and a prior element. This function by default calculates the percentage change from the immediately previous row.