8 examples of 'how to calculate percentage in python' in Python

Every line of 'how to calculate percentage in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
172def get_percentage(i, n):
173 if n == 0:
174 return '0 %'
175 else:
176 v = 100 * (1.0 * i / n)
177 return '%.1f %%' % v
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

47@staticmethod
48def percentage(part: float, whole: float) -> float:
49 """Returns percentage.
50
51 Just a normal algorithm to return the percent.
52
53 Args:
54 part -> part of the whole number
55 whole -> the whole number
56
57 Returns:
58 Returns the percentage of part to whole.
59
60 """
61 if part <= 0 or whole <= 0:
62 return 0
63 # works with percentages
64 return 100 * float(part) / float(whole)
8def percent_of(part, whole):
9 """What percent of ``whole`` is ``part``?
10 """
11 # Use float to force true division.
12 return float(part * 100) / whole
93def percent(numerator, denominator):
94 """
95 :param numerator: float
96 Numerator of fraction
97 :param denominator: float
98 Denominator of fraction
99 :return: str
100 Fraction as percentage
101 """
102
103 if denominator == 0:
104 out = "0"
105 else:
106 out = str(int(numerator / float(denominator) * 100))
107
108 return out + "%"
30@property
31def get_percentage(self):
32 return self.percentage
12def get_percent_output (actual_val, max_val, percent_val, **kwargs):
13 output = 0
14 a = (int(max_val) * int(percent_val))/100
15 if (int(actual_val) > a):
16 output = 1
17
18 return output
36def percent(part, whole):
37 """Convert data to percent"""
38 return round(100 * float(part) / float(whole), PRECISION)
228def _calc_percentage(self, format):
229 """ Compiles sizes from numbers in format, handled as percentage """
230 winwidth = self.vim.call('winwidth', 0)
231 pattern = r'([\<\>\.\:\^])(\d+)'
232
233 def calc_percent(obj):
234 percent = round(winwidth * (int(obj.group(2)) / 100))
235 return obj.group(1) + str(percent)
236
237 return re.sub(pattern, calc_percent, self.__formatter)

Related snippets