Need to calculate percentages in your application? This guide covers everything you need to know about percentage calculations via API, including percentage of values, percentage change, and implementation examples in multiple languages.
What is a Percentage?
A percentage is a number expressed as a fraction of 100, represented by the symbol %. Percentages are used everywhere in daily life, from discounts and taxes to statistics and data analysis. Programmatic percentage calculations are essential for e-commerce, finance, and analytics applications.
For example, 25% of 200 is calculated as: (25/100) * 200 = 50
Types of Percentage Calculations
The API supports multiple percentage calculation types:
Percentage of a Value
Calculate what X% of a number is. For example, what is 15% of 500? Answer: 75.
Percentage Change
Calculate the percentage increase or decrease between two values. Essential for tracking growth, performance metrics, and financial analysis.
What Percentage
Determine what percentage one number is of another. For example, 25 is what percent of 200? Answer: 12.5%.
Using the Percentage Calculator API
TinyFn provides a simple endpoint to calculate percentages:
GET https://api.tinyfn.io/v1/math/percentage
Headers: X-API-Key: your-api-key
{
"result": 75,
"calculation": "15% of 500",
"formula": "(15/100) * 500"
}
Parameters
| Parameter | Type | Description |
|---|---|---|
type |
string | Calculation type: "of", "change", or "what" |
percentage |
number | The percentage value |
value |
number | The base value for calculation |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/math/percentage?type=of&percentage=15&value=500',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const { result } = await response.json();
console.log(result); // 75
Python
import requests
response = requests.get(
'https://api.tinyfn.io/v1/math/percentage',
params={'type': 'of', 'percentage': 15, 'value': 500},
headers={'X-API-Key': 'your-api-key'}
)
result = response.json()['result']
print(result) # 75
cURL
curl "https://api.tinyfn.io/v1/math/percentage?type=of&percentage=15&value=500" \
-H "X-API-Key: your-api-key"
Common Use Cases
- E-commerce Discounts: Calculate discount amounts and final prices
- Financial Analysis: Track percentage changes in stocks, revenue, or metrics
- Tax Calculations: Compute tax amounts based on percentage rates
- Grade Calculations: Convert scores to percentages for educational apps
- Analytics Dashboards: Display percentage changes in KPIs and metrics
Best Practices
- Handle precision: Use appropriate decimal places for financial calculations
- Validate inputs: Ensure percentage values are within expected ranges
- Consider rounding: Define rounding rules for consistent results
- Cache results: Cache frequently used calculations to improve performance
Try the Percentage Calculator API
Get your free API key and start calculating percentages in seconds.
Get Free API Key