1. ## decimal to fraction

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

Code:
```<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;

or

3. That will needlessly clutter the name space. Use instead:
Code:
```<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>```

4. That is how I had it written in the first place, but I wanted to make it so it could be called like this:

Which can't be done with what you posted... Is there a different way of doing that besides what I did?

5. 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.

6. 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:

which was my whole point in doing it that way in the first place. Thanks for fixing my code...

...and you made it so it doesn't work like this:

which was my whole point in doing it that way in the first place. Thanks for fixing my code...
Fair enough, but the 'var' I put in every now and again is rather important.

8. Hi,

I've now got the following script:

Code:
```<html>

<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>

<body>
<input type="text" name="T1" size="20" id='dec'>
</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

9. Code:
```<html>
<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>

<body>
<input type="text" name="T1" size="20" id='dec'>
</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...

10. Thanks, I can't find any prob's now, the code works perfectly.

Thanks,

Neil

11. No problem. I'm glad to help.