This calculator helps you convert between different common rates, such as percentage, decimal, and fractional representations.
function convertRates() {
var decimalValue = parseFloat(document.getElementById("decimalValue").value);
var percentageValue = parseFloat(document.getElementById("percentageValue").value);
var numerator = parseFloat(document.getElementById("numerator").value);
var denominator = parseFloat(document.getElementById("denominator").value);
var resultDiv = document.getElementById("result");
resultDiv.innerHTML = ""; // Clear previous results
var convertedDecimal = null;
var convertedPercentage = null;
var convertedNumerator = null;
var convertedDenominator = null;
var conversionError = false;
// Determine the primary input and perform conversions
if (!isNaN(decimalValue)) {
convertedDecimal = decimalValue;
convertedPercentage = decimalValue * 100;
// Simplification of fraction is complex and depends on GCD, so for now, we'll just show a placeholder or convert to a common fraction if possible
// For simplicity, let's try to convert to a fraction with denominator 100 if it's a terminating decimal, or just show it as a fraction of itself
if (Number.isInteger(decimalValue * 100)) { // e.g. 0.75 -> 75/100
convertedNumerator = decimalValue * 100;
convertedDenominator = 100;
} else {
// Attempt to find a simpler fraction if possible, or just represent it directly
// For this example, we'll just indicate it's not a simple common fraction or show it as N/1
convertedNumerator = decimalValue; // Not ideal as it's not an integer numerator
convertedDenominator = 1;
}
} else if (!isNaN(percentageValue)) {
convertedPercentage = percentageValue;
convertedDecimal = percentageValue / 100;
if (Number.isInteger(convertedDecimal * 100)) {
convertedNumerator = convertedDecimal * 100;
convertedDenominator = 100;
} else {
convertedNumerator = convertedDecimal;
convertedDenominator = 1;
}
} else if (!isNaN(numerator) && !isNaN(denominator) && denominator !== 0) {
convertedNumerator = numerator;
convertedDenominator = denominator;
convertedDecimal = numerator / denominator;
convertedPercentage = convertedDecimal * 100;
} else {
resultDiv.innerHTML = "Please enter at least one valid set of values.";
conversionError = true;
}
if (!conversionError) {
var outputHTML = "