www.webdeveloper.com
Results 1 to 11 of 11

Thread: decimal to fraction

  1. #1
    Join Date
    Jun 2003
    Location
    UK
    Posts
    789

    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

  2. #2
    Join Date
    Dec 2002
    Location
    WI, United States
    Posts
    1,372
    Put this in the head of your page:
    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;
    alert(x.fraction());

    or

    alert(fraction(.75));
    if (succeed != true)
    function askAdam();

    Check out my site!

  3. #3
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    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>
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  4. #4
    Join Date
    Dec 2002
    Location
    WI, United States
    Posts
    1,372
    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?
    if (succeed != true)
    function askAdam();

    Check out my site!

  5. #5
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    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.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  6. #6
    Join Date
    Dec 2002
    Location
    WI, United States
    Posts
    1,372
    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. Thanks for fixing my code...
    if (succeed != true)
    function askAdam();

    Check out my site!

  7. #7
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    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. Thanks for fixing my code...
    Fair enough, but the 'var' I put in every now and again is rather important.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  8. #8
    Join Date
    Jun 2003
    Location
    UK
    Posts
    789
    Hi,

    I've now got the following script:

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

  9. #9
    Join Date
    Dec 2002
    Location
    WI, United States
    Posts
    1,372
    Code:
    <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...
    if (succeed != true)
    function askAdam();

    Check out my site!

  10. #10
    Join Date
    Jun 2003
    Location
    UK
    Posts
    789
    Thanks, I can't find any prob's now, the code works perfectly.

    Thanks,

    Neil

  11. #11
    Join Date
    Dec 2002
    Location
    WI, United States
    Posts
    1,372
    No problem. I'm glad to help.
    if (succeed != true)
    function askAdam();

    Check out my site!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles