Percentage

Report a typo

Implement a function called get_percentage() that converts a real number to a percentage string.

The function should:

  1. Take two parameters:

    • A required real number to be converted to a percentage

    • An optional round_digits parameter for specifying precision (default should round to the nearest integer)

  2. Return a string representing the percentage, including the '%' symbol

  3. Use the built-in round() function for precision control. The function takes up to two arguments, number and ndigits.

Function behavior:

  • If round_digits is not specified, round to the nearest integer

  • If round_digits is specified, round to that many decimal places

  • Always append '%' to the result

Examples (# indicates a comment and is not a part of the result string):

print(get_percentage(0.0123))      # 1%
print(get_percentage(0.0123, 0))   # 1.0%
print(get_percentage(0.0123, 1))   # 1.2%
print(get_percentage(0.0123, 10))  # 1.23%
print(get_percentage(0.0296, 1))   # 3.0%

Just implement the function, you don't have to handle input or call it in your code.

Write a program in Python 3
def get_percentage():
pass
___

Create a free account to access the full topic