Click to See Complete Forum and Search --> : decimal to fraction
neil9999
09-25-2003, 11:39 AM
Hi,
Is it possible to change a decimal (it will always be less than 1 and more than 0) to a fraction in its most simplified form?
Eg. var dec='0.75'
The script would make var frac='3/4'
Thanks,
Neil
AdamBrill
09-25-2003, 12:38 PM
Put this in the head of your page:<script type="text/javascript">
Number.prototype.fraction=fraction;
function fraction(decimal){
if(!decimal){
decimal=this;
}
whole = String(decimal).split('.')[0];
decimal = parseFloat("."+String(decimal).split('.')[1]);
num = "1";
for(z=0; z<String(decimal).length-2; z++){
num += "0";
}
decimal = 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;
}
}
return ((whole==0)?"" : whole+" ")+decimal+"/"+num;
}
</script>You can run it either of these two ways:
x=.75;
alert(x.fraction());
or
alert(fraction(.75));
Charles
09-25-2003, 01:43 PM
That will needlessly clutter the name space. Use instead:<script type="text/javascript">
Number.prototype.fraction = function (decimal){
if(!decimal){
decimal=this;
}
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 = decimal*num;
num = parseInt(num);
for(var z=2; z<decimal+1; z++){
if(decimal%z==0 && num%z==0){
decimal = decimal/z;
num = num/z;
z=2;
}
}
return ((whole==0)?"" : whole+" ")+decimal+"/"+num;
}
</script>
AdamBrill
09-25-2003, 02:25 PM
That is how I had it written in the first place, but I wanted to make it so it could be called like this:
alert(fraction(10.5));
Which can't be done with what you posted... Is there a different way of doing that besides what I did?
Charles
09-25-2003, 02:30 PM
All I did was clean up your function using a function literal, to avoid the unnecessary use of the "function" name, and I made the variables you are using in the function local.
AdamBrill
09-25-2003, 03:08 PM
Originally posted by Charles
All I did was clean up your function using a function literal, to avoid the unnecessary use of the "function" name, and I made the variables you are using in the function local. ...and you made it so it doesn't work like this:
alert(decimal(.5));
which was my whole point in doing it that way in the first place. :rolleyes: Thanks for fixing my code... :rolleyes:
Charles
09-25-2003, 03:13 PM
Originally posted by AdamBrill
...and you made it so it doesn't work like this:
alert(decimal(.5));
which was my whole point in doing it that way in the first place. :rolleyes: Thanks for fixing my code... :rolleyes: Fair enough, but the 'var' I put in every now and again is rather important.
neil9999
09-26-2003, 10:45 AM
Hi,
I've now got the following script:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 1</title>
<script type="text/javascript">
Number.prototype.fraction=fraction;
function fraction(){
decimal=document.getElementById('dec').value
if(!decimal){
decimal=this;
}
whole = String(decimal).split('.')[0];
decimal = parseFloat("."+String(decimal).split('.')[1]);
num = "1";
for(z=0; z<String(decimal).length-2; z++){
num += "0";
}
decimal = 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;
}
}
return ((whole==0)?"" : whole+" ")+decimal+"/"+num;
}
</script>
</head>
<body>
<input type="text" name="T1" size="20" id='dec'>
<button onclick='alert(fraction());'>Change to fraction</button>
</body>
</html>
With this, you enter a decimal into the textbox and click a button, and the fraction pops up. It works for most factions, but for some reason it doesn't work with 0.55
Any ideas why 0.55 doesn't work?
Thanks,
Neil
AdamBrill
09-26-2003, 11:29 AM
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 1</title>
<script type="text/javascript">
Number.prototype.fraction=fraction;
function fraction(){
decimal=document.getElementById('dec').value
if(!decimal){
decimal=this;
}
whole = String(decimal).split('.')[0];
decimal = parseFloat("."+String(decimal).split('.')[1]);
num = "1";
for(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;
}
}
return ((whole==0)?"" : whole+" ")+decimal+"/"+num;
}
</script>
</head>
<body>
<input type="text" name="T1" size="20" id='dec'>
<button onclick='alert(fraction());'>Change to fraction</button>
</body>
</html>For some reason, the math in JavaScript is not always correct... I don't know why, but it sure is a pain. ;) This should work fine now, though... :)
neil9999
09-26-2003, 12:04 PM
Thanks, I can't find any prob's now, the code works perfectly.
Thanks,
Neil
AdamBrill
09-26-2003, 12:39 PM
No problem. I'm glad to help. :)