## Main Page Content

# Formulating Formatted Fractions With Php

## I had this idea...

As a maker of things, and a beginner starting to monkey with PHP, I began to wonder what its usefulness might be for constructing a "practical" application. That is, one that has a use in the real world, for something tangible, rather than as a system for building on-line shopping cart worlds and elaborately confusing web pages. PHP has a lot of tools available to it, including the stunningly useful image creation library GD, as well as a pretty nice bunch of math functions. It sounded promising.## The real world intrudes

One stumbling block that I immediately ran afoul of was fractions. Anything built in the good old US of A that doesn't add up to a whole number of inches is almost always calculated with the sadistically archaic British invention of fractional inches. (Note here that the British have long since abandoned this silliness in favour of the much more sensible metric system).Searching for fractions on several PHP oriented tech sites left me with almost nothing useful, so I couldn't follow my usual method of just stealing somebody else's ideas. Either this is not a subject that regular geeks find interesting, or I'm breaking new ground. Or wind. Either way, and despite being a little intimidated by the technology, the solution was easier than I'd imagined.## Cosmic alignment

Another part of the puzzle was alignment of numbers. And if you're going to generate random numbers, it would be awfully useful to line them up properly. Since` align="char" `

doesn't seem to work yet in HTML, it seems necessary to use tables (egad!) to make numbers align properly. Since we have to break down the numbers anyway to extract the fractions, this doesn't turn out to be any more work at all. ## The Code

Since any further explanation by me will probably be less enlightening than just reading the code and messing with it, I will leave you to do that. With any luck, the following will generate a dynamically sized table with aligned fractions from an array of random numbers. Since I don't care about fractions less than 1/16, this rounds them into oblivion. Also, it sorts the list from long to short and collates multiples in the array. One bit of cleverness here that might not be obvious: I'm using*"1 bitwise and"*

`(1 &)`

to determine odd numbers. If you don't have a good head for math (like me) that might take a moment to sink in. Think binary. If you copy and paste this code, try refreshing the page a few times to see how the table adjusts itself. <?php// get some numbers to play with

$x = rand(0, 130000)/10;

$y = rand(0, 1200);$z = rand(0, 4)/64;$array = array($x, $x, $x, $y, $y, $z, sqrt(2), pi(), pi());// functions

function mult($n) { return intval (round ($n*16)); }

function frac($num) { $mod = fmod ($num, 1)*16;

if (1 & $mod) { return " - ".$mod."/16"; } else $mod = $mod/2;if (1 & $mod) { return " - ".$mod."/8"; }else $mod = $mod/2;if (1 & $mod) { return " - ".$mod."/4"; }else $mod = $mod/2;if (1 & $mod) { return " - ".$mod."/2"; }}// make a table

echo '<table>';

$array = array_map("mult", $array);$array = (array_filter($array, strval)); //get rid of zeros$array = (array_count_values ($array));krsort ($array);while (list ($key, $val) = each ($array)) {$key = $key/16;echo "<tr><td>$val</td> <td> @ </td> <td align=\"right\">".intval($key)." </td> <td> ".frac($key)." </td></tr>";}echo '</table>';?>

## Example Output

3 | @ | 10241 | - 5/8 |

2 | @ | 1062 | |

2 | @ | 3 | - 1/8 |

1 | @ | 1 | - 7/16 |

1 | @ | 0 | - 1/16 |