Main Page Content
Working With Fractions In Css And Php
Most of us are uncomfortable with using fractions when writting programs. If we encounter a fraction, we will first convert it into a floating point number (with decimals) and proceed from there. Most programming languages would prefer to use 0.5 as opposed to 1/2 because the later might conflict with the syntax of the languages. In this article, I will discuss my attempt to work with fractions in one of the PHP projects that I have done.
Displaying Fractions In HTML
There are a few ways to display fractions in the web. The most common way is just to add a slash between 2 numbers, like so "1/2". There are also special HTML characters that we can make fractions look more professional. Eg. to display 1/2, we can use the HTML code½
. However, not many fractions are supported by HTML. So, you may be in trouble if you want to display 2/9 in HTML. A better way to display a fraction is probably to use the superscript and subscript tags. Using
and
tags, my 2/9 will look a bit better. We can also use a "special slash" ⁄
between 2 numbers instead of a normal slash ( / ). Still not satisfied? With CSS, we can push the limit further. If we define a css like this: .fracNum, .fracDen { font-size: 70%; } .fracNum { vertical-align: 0.5em; } .fracDen { vertical-align: -0.5em; }
and when implemented with the 'sup' and 'sub' tags, The fraction will look even better. The code will be something like: 2⁄9
Converting Decimals To Fraction - A Real Example
I wrote a program that let people frame their pictures online. When I was halfway through, I was told that the program should support imperial measurements (inches). The problem with working in inches is that very few people would say my picture is 6.34 inch width by 9.82 inch height; Most people would say 6 and 3/8 inch by 9 and 3/8 height for example. The biggest problem now is that the framing engine calculates everything in mm. Instead of rewritting the whole engine, I figured out that what I need is just an interface to convert mm to inches. So, if I pass 300mm into a function, it should return me 11 and 3/4 as the answer. Dividing 300mm by 1 inch (25.4mm) will leave me with a long decimal. It would be silly to try and display a fraction for a decimal like 0.1111. The function should be intelligent enough to round off the decimal to something that can be easily displayed as a fraction. For example, if I round off 0.1111 to 0.125, I can display the fraction as 1/8 instead of 1111/100000. Time is running short and I need a simple and sweet solution.Implementation
I would like to go through snipplets of code with explaination first before revealing everything. Feel free to jump to the end of the page and copy the whole function if you just want to implement it in your website. The code is written in PHP.Part 1: Extracting the decimal in inches.
The measurement in mm is first converted to inches(with decimal). The decimal is then isolated from the whole number and stored in avar
called $pDecimal
. Now, I need to store a list of all possible fractions with their mm equivalent in an array. I use intervals of 1/8 for the sake of simplicity.Part 2: Rounding off to the closest inch interval.
This is the most important step in the whole conversion process. The foreach loop creates a new array called$tmpV
which stores the inch fraction as the key and the inch difference as the value. We will now sort $tmpV
with the minimum 'mm' value at the top and the maximum 'mm' value at the bottom using the asort
function. We are extracting the first entry in the array because it stores the minimum 'mm' difference. As you can see, what we are trying to do here is to round $pDecimal
off to the closest decimal value in the $fractionOption
array (ie, 0.125, 0.25, 0.375..etc). We can now safely extract the first key-value pair in $tmpV using the list
function. The $inch
var stores the inch representation that we want.