ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php数学函数,加(bcadd)、减(bcsub)、乘(bcmul)、除(bcdiv)

2020-03-11 15:03:56  阅读:399  来源: 互联网

标签:bcadd bcsub string int bcdiv scale right operand left


 

bcscale() 来设置全局默认的小数位数

bcscale ( int $scale ) : bool

设置所有bc数学函数的未设定情况下得小数点保留位数.

undefined

bcadd — 2个任意精度数字的加法计算

左操作数和右操作数求和 ,scale 用于设置结果中小数点后的小数位数。

bcadd ( string $left_operand , string $right_operand [, int $scale ] ) : string

<?php
$a = '1.234';
$b = '5';
    echo bcadd($a, $b);     // 6
    echo bcadd($a, $b, 4);  // 6.2340
?>

 

bccomp — 比较两个任意精度的数字

bccomp ( string $left_operand , string $right_operand [, int $scale = int ] ) : int

 

<?phpecho bccomp('1', '2') . "\n";   // -1
    echo bccomp('1.00001', '1', 3); // 0
    echo bccomp('1.00001', '1', 5); // 1
?>

 

 

bcdiv — 2个任意精度的数字除法计算

bcdiv ( string $left_operand , string $right_operand [, int $scale = int ] ) : string

<?php
// default scale : 3
    bcscale(3);
    echo bcdiv('105', '6.55957'); // 16.007
// this is the same without bcscale()
    echo bcdiv('105', '6.55957', 3); // 16.007
?>

 

bcmod — 对一个任意精度数字取模

bcmod ( string $left_operand , string $modulus ) : string

 

<?php
    echo bcmod('4', '2'); // 0
    echo bcmod('2', '4'); // 2
?>

 

 

bcmul — 2个任意精度数字乘法计算

bcmul ( string $left_operand , string $right_operand [, int $scale = int ] ) : string

<?php
    echo bcmul('1.34747474747', '35', 3); // 47.161
    echo bcmul('2', '4'); // 8
?>

 

bcpow — 任意精度数字的乘方

bcpow ( string $left_operand , string $right_operand [, int $scale ] ) : string

 

<?php
    echo bcpow('4.2', '3', 2); // 74.08
?>

 

 

bcpowmod----将任意的精确数提高到另一个,再用指定的模量还原

bcpowmod ( string $base , string $exponent , string $modulus [, int $scale = 0 ] ) : string

 

<?php
    $a = bcpowmod($x, $y, $mod);
    $b = bcmod(bcpow($x, $y), $mod);// 
    $a and $b are equal to each other.
?>

 

 

bcscale — 设置所有bc数学函数的默认小数点保留位数

bcscale ( int $scale ) : bool

 

<?php// default 
    scale : 3
    bcscale(3);
    echo bcdiv('105', '6.55957'); // 16.007// this is the same without 
    bcscale()
    echo bcdiv('105', '6.55957', 3); // 16.007
?>

 

 

bcsqrt — 任意精度数字的二次方根

bcsqrt ( string $operand [, int $scale ] ) : string

 

<?php
    echo bcsqrt('2', 3); // 1.414
?>

 

 

bcsub — 2个任意精度数字的减法

bcsub ( string $left_operand , string $right_operand [, int $scale = int ] ) : string

<?php
    $a = '1.234';
    $b = '5';
    echo bcsub($a, $b);     // -3
    echo bcsub($a, $b, 4);  // -3.7660
?>

标签:bcadd,bcsub,string,int,bcdiv,scale,right,operand,left
来源: https://www.cnblogs.com/xiondun/p/12462430.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有