Percentage Calculator
Use this calculator to find percentages in various scenarios, such as calculating a percentage of a number, determining what percentage one number is of another, or finding the percentage change between two values.
Understanding Percentages
A percentage is a number or ratio expressed as a fraction of 100. It is often denoted using the percent sign "%". Percentages are a fundamental concept used across many fields, from finance and statistics to everyday shopping and understanding data.
Types of Percentage Calculations
1. What is X% of Y?
This calculation helps you find a specific portion of a whole. For example, if you want to know what 15% of 200 is, you would use this method.
Formula: (X / 100) * Y
Example: To find 15% of 200:
- Value A (X) = 15
- Value B (Y) = 200
- Calculation: (15 / 100) * 200 = 0.15 * 200 = 30
- Result: 30
2. X is what percentage of Y?
This calculation helps you determine what proportion one number represents of another, expressed as a percentage. For instance, if you scored 30 marks out of a total of 200, you can find your percentage score.
Formula: (X / Y) * 100
Example: To find what percentage 30 is of 200:
- Value A (X) = 30
- Value B (Y) = 200
- Calculation: (30 / 200) * 100 = 0.15 * 100 = 15
- Result: 15%
3. Percentage Change from X to Y?
This calculation measures the relative change between an old value and a new value. It's commonly used to track growth, decline, or price fluctuations.
Formula: ((New Value - Original Value) / Original Value) * 100
Example: To find the percentage change from 100 to 120:
- Value A (Original Value) = 100
- Value B (New Value) = 120
- Change: 120 – 100 = 20
- Calculation: (20 / 100) * 100 = 0.20 * 100 = 20
- Result: 20% increase
If the new value is less than the original, it will be a percentage decrease (e.g., from 100 to 80 is a -20% change, or 20% decrease).
How to Use the Calculator
- Enter your first number into the "Value A" field.
- Enter your second number into the "Value B" field.
- Select the type of percentage calculation you wish to perform from the "Calculation Type" dropdown.
- Click the "Calculate Percentage" button.
- Your result will be displayed below the button.
.percentage-calculator-container {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
max-width: 700px;
margin: 20px auto;
padding: 25px;
border: 1px solid #e0e0e0;
border-radius: 10px;
background-color: #ffffff;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}
.percentage-calculator-container h2 {
color: #333;
text-align: center;
margin-bottom: 20px;
font-size: 28px;
}
.percentage-calculator-container h3 {
color: #555;
margin-top: 30px;
margin-bottom: 15px;
font-size: 22px;
border-bottom: 1px solid #eee;
padding-bottom: 5px;
}
.percentage-calculator-container h4 {
color: #666;
margin-top: 25px;
margin-bottom: 10px;
font-size: 18px;
}
.percentage-calculator-container h5 {
color: #777;
margin-top: 20px;
margin-bottom: 8px;
font-size: 16px;
}
.percentage-calculator-container p {
line-height: 1.6;
color: #444;
margin-bottom: 10px;
}
.calculator-form .form-group {
margin-bottom: 18px;
}
.calculator-form label {
display: block;
margin-bottom: 8px;
font-weight: bold;
color: #555;
}
.calculator-form input[type="number"],
.calculator-form select {
width: calc(100% – 22px);
padding: 12px;
border: 1px solid #ccc;
border-radius: 6px;
font-size: 16px;
box-sizing: border-box;
transition: border-color 0.3s ease;
}
.calculator-form input[type="number"]:focus,
.calculator-form select:focus {
border-color: #007bff;
outline: none;
box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.25);
}
.calculator-form button {
display: block;
width: 100%;
padding: 14px;
background-color: #007bff;
color: white;
border: none;
border-radius: 6px;
font-size: 18px;
font-weight: bold;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.2s ease;
margin-top: 20px;
}
.calculator-form button:hover {
background-color: #0056b3;
transform: translateY(-1px);
}
.result-output {
margin-top: 25px;
padding: 15px;
border: 1px solid #d4edda;
background-color: #d4edda;
color: #155724;
border-radius: 6px;
font-size: 18px;
font-weight: bold;
text-align: center;
min-height: 20px;
}
.result-output.error {
border-color: #f5c6cb;
background-color: #f8d7da;
color: #721c24;
}
.percentage-calculator-container ul {
list-style-type: disc;
margin-left: 20px;
margin-bottom: 10px;
color: #444;
}
.percentage-calculator-container ol {
list-style-type: decimal;
margin-left: 20px;
margin-bottom: 10px;
color: #444;
}
.percentage-calculator-container li {
margin-bottom: 5px;
line-height: 1.5;
}
.percentage-calculator-container code {
background-color: #f0f0f0;
padding: 2px 4px;
border-radius: 4px;
font-family: 'Courier New', Courier, monospace;
color: #c7254e;
}
function calculatePercentage() {
var valueA = parseFloat(document.getElementById('valueA').value);
var valueB = parseFloat(document.getElementById('valueB').value);
var calculationType = document.getElementById('calculationType').value;
var resultOutput = document.getElementById('resultOutput');
var result = ";
resultOutput.className = 'result-output'; // Reset class
if (isNaN(valueA) || isNaN(valueB)) {
result = 'Please enter valid numbers for both Value A and Value B.';
resultOutput.className += ' error';
resultOutput.innerHTML = result;
return;
}
switch (calculationType) {
case 'percentOf':
// What is Value A% of Value B?
// Example: What is 15% of 200? (Value A=15, Value B=200)
var calculatedValue = (valueA / 100) * valueB;
result = 'Value A (' + valueA + '%) of Value B (' + valueB + ') is:
' + calculatedValue.toFixed(2) + '';
break;
case 'whatPercent':
// Value A is what percentage of Value B?
// Example: 30 is what percentage of 200? (Value A=30, Value B=200)
if (valueB === 0) {
result = 'Cannot calculate percentage when Value B is zero.';
resultOutput.className += ' error';
} else {
var percentage = (valueA / valueB) * 100;
result = 'Value A (' + valueA + ') is
' + percentage.toFixed(2) + '% of Value B (' + valueB + ').';
}
break;
case 'percentChange':
// Percentage change from Value A to Value B?
// Example: Percentage change from 100 to 120? (Value A=100, Value B=120)
if (valueA === 0) {
result = 'Cannot calculate percentage change when Original Value (Value A) is zero.';
resultOutput.className += ' error';
} else {
var change = valueB – valueA;
var percentChange = (change / valueA) * 100;
var changeType = percentChange >= 0 ? 'increase' : 'decrease';
result = 'Percentage change from ' + valueA + ' to ' + valueB + ' is:
' + percentChange.toFixed(2) + '% ' + changeType + '.';
}
break;
default:
result = 'An unexpected error occurred. Please select a valid calculation type.';
resultOutput.className += ' error';
break;
}
resultOutput.innerHTML = result;
}