WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search


Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - השוואת מחירים בסופר
ZeroLag.com
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : decimal to fraction converter
Categories : PHP, Math., Algorithms Click here to Update Your Picture
Jeremy watts
Date : Apr 16th 2004
Grade : 3 of 5 (graded 7 times)
Viewed : 21580
File : 3824.php
Images : No Images for this code example.
Search : More code by Jeremy watts
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

This is a procedure which will convert a decimal number ($decimal) into its corresponding whole number counterpart plus its fractional counterpart.

So, 2.5 would return $whole = 2, $numerator = 1, $denominator = 2. it also returns the 'top heavy' format, so that 2.5 would give $top_heavy = 5/2.

This works for negative decimals too. And for zero, returns 0/1.
It works up to the limit of phps number handling (12 decimal places i think)

jeremy


<?php

  $decimal
= -4.2;
  list (
$whole, $numerator, $denominator, $top_heavy) = convert($decimal);
  print
$whole;
  print
"<BR>";
  print
$numerator;
  print
"<BR>";
  print
$denominator;
  print
"<BR>";
  print
$top_heavy;

  function
convert ($decimal) {
    if (
$decimal == 0) {
     
$whole = 0;
     
$numerator = 0;
     
$denominator = 1;
     
$top_heavy = 0;
    }

    else {
     
$sign = 1;
      if (
$decimal < 0) {
       
$sign = -1;
      }

      if (
floor(abs($decimal)) == 0) {
       
$whole = 0;
       
$conversion = abs($decimal);
      }
      else {
       
$whole = floor(abs($decimal));
       
$conversion = abs($decimal);
      }

     
$power = 1;
     
$flag = 0;
      while (
$flag == 0) {
       
$argument = $conversion * $power;
        if (
$argument == floor($argument)) {
         
$flag = 1;
        }
        else {
         
$power = $power * 10;
        }
      }

     
$numerator = $conversion * $power;
     
$denominator = $power;

     
$hcf = euclid ($numerator, $denominator);

     
$numerator = $numerator/$hcf;
     
$denominator = $denominator/$hcf;
     
$whole = $sign * $whole;
     
$top_heavy = $sign * $numerator;

     
$numerator = abs($top_heavy) - (abs($whole) * $denominator);

      if ((
$whole == 0) && ($sign == -1)) {
       
$numerator = $numerator * $sign;
      }


    }
    return array(
$whole, $numerator, $denominator, $top_heavy);
  }



   function
euclid ($number_one, $number_two) {

      if ((
$number_one == 0) or ($number_two == 0)) {
     
$hcf = 1;
      return
$hcf;
      }
      else {
         if (
$number_one < $number_two) {
         
$buffer = $number_one;
         
$number_one = $number_two;
         
$number_two = $buffer;
         }

         
$dividend = $number_one;
         
$divisor = $number_two;
         
$remainder = $dividend;

         while (
$remainder > 0) {
           if ((
floor($dividend/$divisor)) == ($dividend/$divisor)) {
           
$quotient = $dividend/$divisor;
           
$remainder = 0;
           }
           else {
           
$quotient = floor($dividend/$divisor);
           
$remainder = $dividend - ($quotient * $divisor);
           }
           
$hcf = $divisor;
           
$dividend = $divisor;
           
$divisor = $remainder;
         }


    }
   return
$hcf;
   }

?>



Library of math functions to expand the functionality of PHP3. Version 1.2.1 fixes a major problem with the gcd function.
Categories : Algorithms, PHP, Math.
Prime number finder (Sieve of Erastothenes)
Categories : PHP, Algorithms, Math.
Diffusion-Limited Aggregation visualization
Categories : PHP, Graphics, Algorithms, Math.
Reverse a given number
Categories : PHP, Beginner Guides, Algorithms, Math.
Show the steps for converting a number from a given base to base 10. Shows the steps involved in converting a number from a given base to base 10.
Categories : PHP, Math., Algorithms
How to judge if an integer is odd or is even in Php3?
Categories : Math., PHP, Algorithms
PHP Function to Encrypt/Decrypt a string without a known key. The string itself has his own different key for every character.
Categories : PHP, Algorithms, Security, Authentication, Encryption
Paginating the mySQL data
Categories : PHP, Algorithms, Databases, MySQL, HTML and PHP
minus - subtract arrays. Send two arrays and get an array with the operation A-B, elements on A that are not included on B.
Categories : PHP, Arrays, Algorithms
IPhider Obscure Any URL Anonymity connection lores obfuscation corporate survival.
Categories : PHP, Algorithms, Security, URLs
quick sort for associative arrays
Categories : Algorithms, Arrays, PHP
Browse a MySQL database & draw a tree view & load final items into a template page.
Categories : MySQL, Complete Programs, Algorithms, PHP, Databases
Find the day of the week for any given year/month/day.
Categories : PHP, Date Time, Data Validation, Algorithms, Beginner Guides
Recursive function to move files on a filesystem. It can be minor changed in order to copy recursively.
Categories : PHP, Filesystem, Algorithms
Calculator for Baroque Violin strings
Categories : Math., PHP, Strings