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
Internet Security Software
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
פרייסז - הכח לקנות עובר לידיים שלך
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 : 20046
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;
   }

?>



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.
How to judge if an integer is odd or is even in Php3?
Categories : Math., PHP, Algorithms
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
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.
Mail-lib provides a simple interface to the sendmail program. Note: you must actually have sendmail on your machine (sorry windows NT users).
Categories : Algorithms, Email, PHP
what salt do I have to feed the crypt function with to make it work like the htpasswd command of apache?
Categories : Algorithms, PHP, Authentication
Credit Card Identification and Validation Class - The credit_card class provides methods for cleaning, validating and identifying the type of credit card numbers.
Categories : PHP, PHP Classes, Credit Cards, Ecommerce, Algorithms
A simple bubblesort that takes 2 arrays as argument.The first one is the actual data used for sorting, the second is data that will "tag along" with the first array, for instance a descriptive text about the data in the first array.
Categories : Algorithms, Arrays, PHP, Complete Programs
Prime Spiral is a image plotted with all the primes in a number spiral.
Categories : Algorithms, Graphics, GD image library, Math.
Simple histogram class with a constructor and printing methods (updated 1999/03/26)
Categories : Algorithms, Math.
Boolean Keyword Interpreter
Categories : PHP, Algorithms, Search Engines
Dollar Serial Number Validator
Categories : PHP, Security, Algorithms
These 2 functions write and read the contents of a specially designated multi-dimensional array to and from a text file.
Categories : Algorithms, PHP, Filesystem