Kodeclik Blog
How to convert decimal to percentage
There are many situations, e.g., in financial calculations (interest rates, profit margins) and in academics (est scores, GPAs, averages) where you will need to convert decimals to percentages.
For instance, you know 0.25 can be presented as 25%. How do we do this in Python? There are at least three methods you can exploit.
Method 1: Use Simple Multiplication
This straightforward approach multiplies the decimal by 100 and appends the percentage symbol. It's ideal for basic calculations where precision isn't critical. For instance, consider:
decimal = 0.75
percentage = decimal * 100
print(f"{percentage}%")
For the variable “decimal” (which is initialized to 0.75 or three-quarters), we perform the conversion by multiplying the decimal by 100, storing the result in the percentage variable. The multiplication by 100 shifts the decimal point two places to the right, transforming 0.75 into 75.0. Finally, we use an f-string (formatted string literal) to print the result, where f"{percentage}%" combines the numerical value with the percentage symbol.
The output will be:
75.0%
Method 2: Use String Formatting
In this approach, we use the format() method which provides a clean way to convert decimals to percentages with specified precision. Consider:
decimal = 0.2567
formatted_percentage = "{:.2%}".format(decimal)
print(formatted_percentage)
This code demonstrates a more sophisticated approach to decimal-to-percentage conversion using Python's string formatting method. The key feature here is the "{:.2%}".format(decimal) syntax, where .2% is a special format specifier that performs two operations simultaneously: it multiplies the decimal by 100 automatically (converting it to a percentage) and rounds the result to exactly 2 decimal places (specified by .2).
The output is:
25.67%
This formatting approach is particularly useful in financial applications, scientific calculations, or any scenario where precise control over decimal places is required, such as displaying interest rates, statistical results, or measurement tolerances. The .format() method provides more control over the output format compared to simple multiplication, making it ideal for professional applications where consistent decimal precision is important.
Method 3: Use f-strings
Finally, F-strings offer the most readable and concise syntax for percentage formatting. In other words, they can do the computation and formatting in one step (unlike what we saw in Method 1):
probability = 1/3
print(f"{probability:.1%}")
As you can see the above code is an elegant way to convert a fraction to a percentage using Python's f-strings (btw, f-strings stand for “formatted string literals”). Here, probability = 1/3 creates a decimal value by dividing 1 by 3, resulting in approximately 0.3333333... The f-string formatting f"{probability:.1%}" performs several operations: it automatically multiplies the decimal by 100, rounds the result to one decimal place (specified by .1), and appends the % symbol. Thus, the .1% specification controls decimal precision while handling the multiplication automatically.
The output will be:
33.3%
This approach is perfect for scenarios like displaying statistical probabilities, voting results, or sports statistics where a single decimal place is sufficient. The f-string syntax, introduced in Python 3.6+, is considered more readable and maintainable than older formatting methods, and it's especially useful when you need to embed formatted numbers within larger strings or when working with probability calculations that naturally occur as fractions.
Now that you have seen several examples of converting decimals to percentages, let us now look at some applications where we will need to convert decimal to percentages.
Converting decimal to percentage for a grade calculator
Here is a “grade calculator” program that converts raw scores to percentage grades, useful in educational settings. It handles the division and formatting in one step:
def calculate_grade_percentage(score, total):
decimal = score / total
return f"{decimal:.1%}"
student_score = 85
total_points = 100
print(f"Student grade: {calculate_grade_percentage(student_score, total_points)}")
This program uses the formatted f-string approach to obtain the desired output which will be:
Student grade: 85.0%
Converting decimal to percentage for a financial calculator
Here is a second example. This is a program that calculates profit margins as percentages, essential for business analysis. It demonstrates how percentage calculations can provide meaningful business metrics.
def calculate_profit_margin(revenue, cost):
margin = (revenue - cost) / revenue
return f"Profit Margin: {margin:.2%}"
revenue = 50000
cost = 35000
print(calculate_profit_margin(revenue, cost))
The output will be:
Profit Margin: 30.00%
Converting decimal to percentage for statistical analysis
Finally, the below program computes success rates from experimental data, commonly used in scientific research and quality control. It shows how percentages can represent proportional outcomes.
def calculate_success_rate(successes, attempts):
rate = successes / attempts
percentage = f"{rate:.1%}"
return f"Success Rate: {percentage}"
successful_trials = 45
total_trials = 50
print(calculate_success_rate(successful_trials, total_trials))
The output will be:
Success Rate: 90.0%
If you liked this blogpost, checkout how to compute percentiles in Python!
Want to learn Python with us? Sign up for 1:1 or small group classes.