Re: Thoughts on Feature Request - Arithmetic

From: Date: Fri, 07 Dec 2007 17:37:13 +0000
Subject: Re: Thoughts on Feature Request - Arithmetic
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-33840@lists.php.net to get a copy of this message
Yes, an easy way to handle this functionality that is safe to use with user input would be REALLY nice. Specifically for allowing users to specify custom formulas to do all sorts of nifty stuff. Especially in reporting and payroll/commission applications. We're currently working on a reporting class where we would like the user to able to specify custom columns in XML, ie: $col5 = ($col1 / $col2) * ($col3 / $col4) Or in our payroll application where customers want to calculate custom commissions for sales people: $commission = ($gross_sales - $gross_pay - $medical_benefits - $chargebacks) * $employee_commission_bracket On Fri, 2007-12-07 at 14:51 +0000, Nathan Rixham wrote: > In-Built PHP Functions for parsing of basic arithmetic and if possible > fraction to decimal and decimal to fraction > > $arithmetic_string = "3*5"; > echo arith($arithmetic_string); // returns float 15 > > $arithmetic_string = "1/2"; > echo arith($arithmetic_string); // returns float 0.5 > > $fraction_string = "1/4"; > echo fracdec($fraction_string); // returns float 0.25 > > $dec_string = "0.5"; > echo decfrac($dec_string); // returns string "1/4" > > I've used php for years, came accross the need for this today and was > suprised such a basic command wasn't there.. currently the only way I > can see to do it would be to eval.. which isn't the most secure method > for such a basic need. > > Regards > > Nathan > -- Mike <ipso@snappymail.ca>

Attachment: [application/pgp-signature] This is a digitally signed message part signature.asc
« previous php.internals (#33840) next »