phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


PHP - Number: Working With Fractions

Tag: number, fraction   Category: PHP Basic
post: 16 Mar 2008 read: 65


If you want to perform a mathematical operation involving fractions, you can use PEAR's Math_Fraction class as follows:

<?php
// include Math_Fraction class
include "Math/Fraction.php";

// define a new fraction
$fract = new Math_Fraction(1,2);

// print as string
// result: "1/2 "
echo $fract->toString() . " \n";

// print as float
// result: 0.5
echo $fract->toFloat()
?>

A fraction is a number in the form a/b and in this form, a is called the numerator and b is called the denominator. The denominator of a fraction can never be 0. Examples of fractions include 1/3, 19/7, and 1.5/3.5. A fraction here is expressed as an object and generated by passing the fraction's numerator and denominator as arguments to the class constructor. Two methods such as toString( ) and toFloat( ) take cares of displaying the fraction, either as a fraction or a floating-point value.




| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting