站长论坛

标题: PHP函数bcdiv()高精度数学函数 [打印本页]

作者: superadmin    时间: 2008-3-31 15:21
标题: PHP函数bcdiv()高精度数学函数
bcdiv
(PHP 4, PHP 5)

bcdiv — Divide two arbitrary precision numbers

说明
string bcdiv ( string $left_operand, string $right_operand [, int $scale] )

Divides the left_operand by the right_operand.

参数

left_operand
The left operand, as a string.

right_operand
The right operand, as a string.

scale
This optional parameter is used to set the number of digits after the decimal place in the result. You can also set the global default scale for all functions by using bcscale().


返回值
Returns the result of the division as a string, or NULL if right_operand is 0.

范例
例 312. bcdiv() example

<?php

echo bcdiv('105', '6.55957', 3);  // 16.007

?>


参见
bcmul()





User Contributed Notes bcdiv
Gautam
29-Aug-2007 10:56
<?php
//converting in to required precision of decimal points
$result= bcdiv(89.99999999997,2.57865741235478,2);
echo "$result";  // 34.90 result with 2 decimal points
?>
cristianDOTzuddas]NOSPAM[gmailDOTcom
24-Jul-2005 08:10
Decimal to binary conversion, using BC Math.
Note: this function is VERY slow if the decimal number is too big!

<?
function bc_decbin($dec_str) {
    if (strlen($dec_str)>0) {
        $bin_str = '';
        do {
            if (((int)$dec_str[strlen($dec_str)-1] % 2) === 0)
                $bin_str .= '0';
            else
                $bin_str .= '1';
            
            $dec_str = bcdiv($dec_str, '2');
        } while ($dec_str!='0');
        
        return strrev($bin_str);
    }
    else
        return null;
}
?>




欢迎光临 站长论坛 (http://www.tzlink.com/bbs/) Powered by Discuz! X3.2