Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added vice versa in Percentage To Fraction Calculator #579

Merged
merged 6 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
# Percentage To Fraction Calculator

This is a simple online calculator converts a percent to a fraction

## Features

- Converts Percent to Fraction.
- It also provide detail solution
- Easy-to-use command-line interface.
- You can reset the calculation to starting

## Example

For example, to calculate the fraction of percentage 62.5%:
- Enter 62.5
- Click "Convert" to see the result.

## Tech Stack

- HTML
- CSS
- JavaScript

## Screenshots
![Alt text](image.png)
# Percentage Fraction Calculator

This is a simple online calculator converts a percent to a fraction and vice-versa

## Features

- Converts Percent to Fraction or Fraction to Percent.
- It also provide detail solution
- You can reset the calculation to starting

## Example

For example, to calculate the fraction of percentage 62.5%:
- Enter 62.5
- Click "Convert" to see the result.

## Tech Stack

- HTML
- CSS
- JavaScript

## Screenshots
![Alt text](image.png)
46 changes: 46 additions & 0 deletions Calculators/Percentage-Fraction-Calculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Percentage Fraction Calculator</title>
</head>

<body>

<div class="calculator">
<h1>Percentage Fraction Calculator</h1>

<!-- Percentage input section -->
<div class="inp">
<label for="percentInput">Enter a Percentage:</label>
<input type="text" id="percentInput" placeholder="Enter percent">
<button onclick="convertPercentToFraction()">Convert to Fraction</button>
</div>

<!-- Fraction input section -->
<div class="inp">
<label for="fractionInput">Enter a Fraction:</label>
<input type="text" id="fractionInput" placeholder="Enter fraction (e.g., 3/4)">
<button onclick="convertFractionToPercent()">Convert to Percentage</button>
</div>

<!-- Result display -->
<div id="result">
<p class="answer">Answer:</p>
<p class="resultValue" id="resultValue"></p>
<p class="showingWorkText">Showing Work:</p>
<p class="showingWork" id="showingWork"></p>
</div>

<!-- Clear button -->
<button id="clearButton" onclick="clearInputs()">Clear</button>
</div>

<script src="script.js"></script>

</body>

</html>
106 changes: 106 additions & 0 deletions Calculators/Percentage-Fraction-Calculator/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
window.convertPercentToFraction = function () {
var percentInput = document.getElementById('percentInput').value;

// Check if the input is empty or not a valid number
if (percentInput.trim() === '' || isNaN(percentInput)) {
alert('Please enter a valid number.');
return;
}

// Convert percent to fraction
var decimalValue = parseFloat(percentInput) / 100;
var fraction = decimalToFraction(decimalValue);

// Display the result
document.getElementById('resultValue').innerText = '= ' + fraction.numerator + '/' + fraction.denominator;
document.getElementById('showingWork').innerText =
percentInput + '% = ' + percentInput + '/100 = ' + decimalValue + ' = ' + fraction.numerator + '/' + fraction.denominator;
};

window.convertFractionToPercent = function () {
var fractionInput = document.getElementById('fractionInput').value;

// Check if the input is empty or not a valid fraction
if (fractionInput.trim() === '' || !isValidFraction(fractionInput)) {
alert('Please enter a valid fraction.');
return;
}

// Split fraction into numerator and denominator
var parts = fractionInput.split('/');
var numerator = parseInt(parts[0]);
var denominator = parseInt(parts[1]);

// Check if denominator is zero
if (denominator === 0) {
alert('Denominator cannot be zero. Please enter a valid fraction.');
return;
}

// Calculate percentage
var percent = (numerator / denominator) * 100;

// Simplify fraction
var gcd = findGCD(numerator, denominator);
var simplifiedNumerator = numerator / gcd;
var simplifiedDenominator = denominator / gcd;

// Display the result
document.getElementById('resultValue').innerText = '= ' + percent.toFixed(2) + '%';

// Showing work steps
var workSteps = fractionInput + ' = ' + simplifiedNumerator + '/' + simplifiedDenominator;
workSteps += ' = ' + (simplifiedNumerator / simplifiedDenominator).toFixed(2);
workSteps += ' = ' + percent.toFixed(2) + '%';

// Display the work steps
document.getElementById('showingWork').innerText = workSteps;
};

// Function to find the greatest common divisor (GCD) of two numbers
function findGCD(a, b) {
return b === 0 ? a : findGCD(b, a % b);
}


window.clearInputs = function () {
document.getElementById('percentInput').value = '';
document.getElementById('fractionInput').value = '';
document.getElementById('resultValue').innerText = '';
document.getElementById('showingWork').innerText = '';
};

// Function to convert decimal to fraction
function decimalToFraction(decimalValue) {
const tolerance = 1.0e-9;

for (var denominator = 1; ; ++denominator) {
var numerator = Math.round(decimalValue * denominator);

if (Math.abs(decimalValue - numerator / denominator) < tolerance) {
return {
numerator: numerator,
denominator: denominator,
};
}
}
}

// Function to check if the input is a valid fraction
function isValidFraction(input) {
return /^-?\d+\/\d+$/.test(input);
}

// Function to convert fraction to percent
function fractionToPercent(fraction) {
var parts = fraction.split('/');
var numerator = parseInt(parts[0]);
var denominator = parseInt(parts[1]);

// Check if denominator is zero
if (denominator === 0) {
return NaN; // Return NaN to indicate invalid input
}

return (numerator / denominator) * 100;
}
Loading
Loading