Page 1 of 1

remove extra number

Posted: Fri Sep 23, 2005 7:08 pm
by toyonsol
I'm performing different math functions on a specific number and after i get the final result how do i remove any extra numbers that are more than two to the right of the decimal.

For Example:

I get this number: 780.00195
how do I make it only show 780.00

Any help would be greatly appreciated.

Posted: Fri Sep 23, 2005 7:13 pm
by feyd

Posted: Fri Sep 23, 2005 7:17 pm
by toyonsol
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


thanks thats exactlyed what i needed.

Code: Select all

function format_number($str,$decimal_places='2',$decimal_padding="0"){
       /* firstly format number and shorten any extra decimal places */
       /* Note this will round off the number pre-format $str if you dont want this fucntionality */
       $str          =  number_format($str,$decimal_places,'.','');    // will return 12345.67
       $number      = explode('.',$str);
       $number[1]    = (isset($number[1]))?$number[1]:''; // to fix the PHP Notice error if str does not contain a decimal placing.
       $decimal    = str_pad($number[1],$decimal_places,$decimal_padding);
       return (float) $number[0].'.'.$decimal;
}

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]