PHP code example of k6xiao / strtobcmath

1. Go to this page and download the library: Download k6xiao/strtobcmath library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

k6xiao / strtobcmath example snippets




// @param $scale    // 可选,精度,默认值:8
// @param $isecho   // 可选,是否输出计算过程,默认值:false
$math = new StrToBcmath();

$result = $math->of('(((2.5-3.5)+8)**(5-1)+6)/2');
// a**b 表示a的b次方
echo $result;   // 1203.50000000

// 使用书写案例
$money = 123.456;
$sxf   = 0.6;
$fee   = (new StrToBcmath(6))->of("{$money}*{$sxf}/100");
echo "{$money} 的 {$sxf}% 手续费是:{$fee}";
// 123.456 的 0.6% 手续费是:0.740736



// 定义一些表达式
$expressions = [
    '2*3+6+6/2',
    '2/3',
    '2+3',
    '2-3',
    '2-3**2',
    '(5-3)**2',
    '2.5*3.5',
    '2.5/3.5',
    '2.5+3.5',
    '2.5-3.5',
    '((2.5-3.5)+8)*3',
    '(((2.5-3.5)+8)*(2.5-1)+6)/2',
];

// 计算每个表达式的结果
foreach ($expressions as $expression) {
    $result = (new StrToBcmath(6, true))->of($expression) * 1;
    echo "$expression = $result<br/><br/>";
}