View Full Version : Converting Decimals to Fractions

macleodjb

07-18-2009, 01:39 AM

I know this is a rather odd post but I need to be able to convert a decimal to a fraction.

Really all i need to know to get what i have to work is being able to tell if a number is whole or not.

For instance if i have an accuracy set of .0625 = 1/16"

If i take my variable which is set to .5625 (9/16")

so if i take variable/accuracy in this case would equal 9. So this way i would know that the numerator would be 9 and my denominator would be 16.

If i take the same accuracy of 1/16 and now introduce a variable of 0.5 then it will give me a remainder.

I think i'm getting all jumbled up, if anyone has any insight this would be great.

venegal

07-18-2009, 03:58 AM

Your post would be less odd if you described what you are actually trying to accomplish, and especially why.

But since for now that post is all there is, I'll try to get into that mumbo jumbo:

I know this is a rather odd post but I need to be able to convert a decimal to a fraction.

If that's all you want to do, that's quite easy. .5625 as a fraction is 5625/10000. If you want it in lowest terms, use the euclidian algorithm to compute the greatest common divisor.

Really all i need to know to get what i have to work is being able to tell if a number is whole or not.

If that's all you want to know, that's quite easy too.

number == parseInt(number) will tell you if it's whole.

For instance if i have an accuracy set of .0625 = 1/16"

If i take my variable which is set to .5625 (9/16")

so if i take variable/accuracy in this case would equal 9. So this way i would know that the numerator would be 9 and my denominator would be 16.

Don't know what you are doing there. You won't know your denominator would be 16; you put that information right into that calculation. You don't want to test all natural numbers just to find out the denominator, do you?

If i take the same accuracy of 1/16 and now introduce a variable of 0.5 then it will give me a remainder.

What remainder are you talking about? It will give you 8.

I think i'm getting all jumbled up, if anyone has any insight this would be great.

I'm afraid insight into your problem is exactly what I am lacking. Please provide.

macleodjb

07-18-2009, 04:26 AM

I'm trying to take a given number whether it be .5625 or .3125 or .5 and convert it to an english fractional number such as 9/16, 5/16, or 1/2"

The user can also specify the accuracy for rounding purposes for the number. which was what i was talking about before if the user wants numbers rounded to the nearest 1/16" of an inch or nearest 1/4" I'm going to try what you've wrote already and see if it helps.

Thanks.

Well, just multiply by 16 and round to the nearest integer. That gives you the numerator. 16 would, of course, be the denominator.

Philip M

07-18-2009, 09:27 AM

Try this:-

<html>

<head>

<script type="text/javascript">

var roundTo = 16; // round to nearest 1/roundTo;

function fraction() {

var decimal = document.getElementById('dec').value;

if ((!/^\d*\.\d+$/g.test(decimal)) || (String(decimal).split('.')[1].length > 4)) {

alert ("You must enter a decimal number (max 4 decimal digits)!");

document.getElementById('dec').value = "";

document.getElementById('dec').focus();

return false;

}

var whole = String(decimal).split('.')[0];

decimal = parseFloat("." + String(decimal).split('.')[1]);

var num = "1";

for (var z=0; z<String(decimal).length-2; z++){

num += "0";

}

decimal = parseInt(decimal*num);

num = parseInt(num);

for (z=2; z<decimal+1; z++) {

if (decimal%z==0 && num%z==0) {

decimal = decimal/z;

num = num/z;

z = 2;

}

}

var result = ((whole==0)?"" : whole+" ") + decimal + "/" + num;

alert ("Fractional value = " + result);

var d = num/roundTo;

var n = Math.round(decimal/d);

alert ("Rounded to nearest 1/" + roundTo + " inch = " + ((whole==0)?"" : whole+" ") + n + "/" + roundTo + " inch");

var HCF = 1;

for (z=1; z<100; z++){

if (n%z == 0 && roundTo%z == 0) {

HCF = z;

}

}

var inch = " inch";

var simplified = (n/HCF +'/'+ roundTo/HCF);

if (simplified == "1/1") {

simplified = "";

whole ++;

}

else if (simplified == "0/1") {

simplified = "";

}

if (whole == 1) {inch = "inch"}

else {inch = "inches"}

if (!whole) {inch = "0 inches"}

alert ("Simplified to: " + ((whole==0)?"" : whole + " ") + simplified + inch);

}

</script>

</head>

<body>

<input type="text" name="dec" id= "dec" size="6" >

<button onclick = 'fraction();'>Change decimal to fraction</button>

</body>

</html>

111,111,111 x 111,111,111 = 12,345,678,987,654,321

12 Pack Mack

07-18-2009, 11:39 PM

macleodjb:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>

<title>None</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<script type="text/javascript">

function calcResult(nForm){

var nMeasurement = nForm['measurement'].value;

var roundingFraction = nForm['roundTo'].value;

if (!/^\.\d+$/.test(nMeasurement))

{

alert('Invalid Measurement');

nForm['result'].value = "";

return;

}

if(!/^1\/\d{1,2}$/.test(roundingFraction))

{

alert('Invalid Rounding Fraction');

nForm['result'].value = "";

return;

}

var nMeasureNumerator = nMeasurement.substring(1,nMeasurement.length);

var nMeasureDenominator = Math.pow(10,nMeasureNumerator.length);

var roundingFractionDenominator = roundingFraction.split("/")[1];

var roundingFractionNumerator = Math.round((nMeasureNumerator * roundingFractionDenominator) / nMeasureDenominator);

while (roundingFractionNumerator % 2 == 0 && roundingFractionDenominator % 2 == 0)

{

roundingFractionNumerator = roundingFractionNumerator / 2;

roundingFractionDenominator = roundingFractionDenominator / 2;

}

while (roundingFractionNumerator % 3 == 0 && roundingFractionDenominator % 3 == 0)

{

roundingFractionNumerator = roundingFractionNumerator / 3;

roundingFractionDenominator = roundingFractionDenominator / 3;

}

nForm['result'].value = roundingFractionNumerator + "/" + roundingFractionDenominator;

}

function init(){

var nForm = document.forms[0];

nForm['convert'].onclick = function()

{

calcResult(nForm);

}

}

navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);

</script>

<style type="text/css">

.center {text-align: center;}

</style>

</head>

<body>

<form action="" method="post">

<label>Decimal Measurement: <input type="text" name="measurement" class="center" size="12" value=".5625"></label>

<br><br>

<label>Round to Nearest: <input type="text" name="roundTo" class="center" size="5" value="1/16"></label>

<br><br>

<label>Result: <input type="text" name="result" class="center" size="5" readonly></label>

<br><br>

<input type="button" value="Convert" name="convert">

</form>

</body>

</html>

Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.