浮点数高精度计算

  • bcadd — 2个任意精度数字的加法计算
  • bccomp — 比较两个任意精度的数字
  • bcdiv — 2个任意精度的数字除法计算
  • bcmod — 对一个任意精度数字取模
  • bcmul — 2个任意精度数字乘法计算
  • bcpow — 任意精度数字的乘方
  • bcpowmod — Raise an arbitrary precision number to another, reduced by a specified modulus
  • bcscale — 设置所有bc数学函数的默认小数点保留位数
  • bcsqrt — 任意精度数字的二次方根
  • bcsub — 2个任意精度数字的减法
/**
 * Add two arbitrary precision numbers
 * @link https://php.net/manual/en/function.bcadd.php
 * @param string $left_operand <p>
 * The left operand, as a string.
 * </p>
 * @param string $right_operand <p>
 * The right operand, as a string.
 * </p>
 * @param int $scale [optional] <p>
 * This optional parameter is used to set the number of digits after the
 * decimal place in the result. If omitted, it will default to the scale
 * set globally with the {@link bcscale()} function, or fallback to 0 if
 * this has not been set.
 * </p>
 * @return string The sum of the two operands, as a string.
 * @since 4.0
 * @since 5.0
 * @since 7.0
 */
function bcadd ($left_operand, $right_operand, $scale = 0) {}

/**
 * Subtract one arbitrary precision number from another
 * @link https://php.net/manual/en/function.bcsub.php
 * @param string $left_operand <p>
 * The left operand, as a string.
 * </p>
 * @param string $right_operand <p>
 * The right operand, as a string.
 * </p>
 * @param int $scale [optional] <p>
 * This optional parameter is used to set the number of digits after the
 * decimal place in the result. If omitted, it will default to the scale
 * set globally with the {@link bcscale()} function, or fallback to 0 if
 * this has not been set.
 * </p>
 * @return string The result of the subtraction, as a string.
 * @since 4.0
 * @since 5.0
 * @since 7.0
 */
function bcsub ($left_operand, $right_operand, $scale = 0) {}

/**
 * Multiply two arbitrary precision numbers
 * @link https://php.net/manual/en/function.bcmul.php
 * @param string $left_operand <p>
 * The left operand, as a string.
 * </p>
 * @param string $right_operand <p>
 * The right operand, as a string.
 * </p>
 * @param int $scale [optional] <p>
 * This optional parameter is used to set the number of digits after the
 * decimal place in the result. If omitted, it will default to the scale
 * set globally with the {@link bcscale()} function, or fallback to 0 if
 * this has not been set.
 * </p>
 * @return string the result as a string.
 * @since 4.0
 * @since 5.0
 * @since 7.0
 */
function bcmul ($left_operand, $right_operand, $scale = 0) {}

/**
 * Divide two arbitrary precision numbers
 * @link https://php.net/manual/en/function.bcdiv.php
 * @param string $dividend <p>
 * The dividend, as a string.
 * </p>
 * @param string $divisor <p>
 * The divisor, as a string.
 * </p>
 * @param int $scale [optional] <p>
 * This optional parameter is used to set the number of digits after the
 * decimal place in the result. If omitted, it will default to the scale
 * set globally with the {@link bcscale()} function, or fallback to 0 if
 * this has not been set.
 * </p>
 * @return string|null the result of the division as a string, or <b>NULL</b> if
 * <i>divisor</i> is 0.
 * @since 4.0
 * @since 5.0
 * @since 7.0
 */
function bcdiv ($dividend, $divisor, $scale = 0) {}

/**
 * Get modulus of an arbitrary precision number
 * @link https://php.net/manual/en/function.bcmod.php
 * @param string $dividend <p>
 * The dividend, as a string.
 * </p>
 * @param string $divisor <p>
 * The divisor, as a string.
 * </p>
 * @param int $scale [optional] <p>
 * This optional parameter is used to set the number of digits after the
 * decimal place in the result. If omitted, it will default to the scale
 * set globally with the {@link bcscale()} function, or fallback to 0 if
 * this has not been set.
 * </p>
 * @return string|null the modulus as a string, or <b>NULL</b> if
 * <i>divisor</i> is 0.
 * @since 4.0
 * @since 5.0
 * @since 7.0
 * @since 7.2.0 scale param added, dividend/divisor no longer truncated
 */
function bcmod ($dividend, $divisor, $scale = 0) {}

/**
 * Raise an arbitrary precision number to another
 * @link https://php.net/manual/en/function.bcpow.php
 * @param string $base <p>
 * The base, as a string.
 * </p>
 * @param string $exponent <p>
 * The exponent, as a string. If the exponent is non-integral, it is truncated.
 * The valid range of the exponent is platform specific, but is at least
 * -2147483648 to 2147483647.
 * </p>
 * @param int $scale [optional] <p>
 * This optional parameter is used to set the number of digits after the
 * decimal place in the result. If omitted, it will default to the scale
 * set globally with the {@link bcscale()} function, or fallback to 0 if
 * this has not been set.
 * </p>
 * @return string the result as a string.
 * @since 4.0
 * @since 5.0
 * @since 7.0
 */
function bcpow ($base, $exponent, $scale = 0) {}

/**
 * Get the square root of an arbitrary precision number
 * @link https://php.net/manual/en/function.bcsqrt.php
 * @param string $operand <p>
 * The operand, as a string.
 * </p>
 * @param int $scale [optional]
 * @return string the square root as a string, or <b>NULL</b> if
 * <i>operand</i> is negative.
 * @since 4.0
 * @since 5.0
 * @since 7.0
 */
function bcsqrt ($operand, $scale = null) {}

/**
 * Set default scale parameter for all bc math functions
 * @link https://php.net/manual/en/function.bcscale.php
 * @param int $scale <p>
 * The scale factor.
 * </p>
 * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure.
 * @since 4.0
 * @since 5.0
 * @since 7.0
 */
function bcscale ($scale) {}

/**
 * Compare two arbitrary precision numbers
 * @link https://php.net/manual/en/function.bccomp.php
 * @param string $left_operand <p>
 * The left operand, as a string.
 * </p>
 * @param string $right_operand <p>
 * The right operand, as a string.
 * </p>
 * @param int $scale [optional] <p>
 * The optional <i>scale</i> parameter is used to set the
 * number of digits after the decimal place which will be used in the
 * comparison.
 * </p>
 * @return int 0 if the two operands are equal, 1 if the
 * <i>left_operand</i> is larger than the
 * <i>right_operand</i>, -1 otherwise.
 * @since 4.0
 * @since 5.0
 * @since 7.0
 */
function bccomp ($left_operand, $right_operand, $scale = 0) {}

/**
 * Raise an arbitrary precision number to another, reduced by a specified modulus
 * @link https://php.net/manual/en/function.bcpowmod.php
 * @param string $base <p>
 * The base, as an integral string (i.e. the scale has to be zero).
 * </p>
 * @param string $exponent <p>
 * The exponent, as an non-negative, integral string (i.e. the scale has to be
 * zero).
 * </p>
 * @param string $modulus <p>
 * The modulus, as an integral string (i.e. the scale has to be zero).
 * </p>
 * @param int $scale [optional] <p>
 * This optional parameter is used to set the number of digits after the
 * decimal place in the result. If omitted, it will default to the scale
 * set globally with the {@link bcscale()} function, or fallback to 0 if
 * this has not been set.
 * </p>
 * @return string|null the result as a string, or <b>NULL</b> if <i>modulus</i>
 * is 0 or <i>exponent</i> is negative.
 * @since 5.0
 * @since 7.0
 */
function bcpowmod ($base, $exponent, $modulus, $scale = 0) {}

常用数值处理方案

舍去法取整(向下取整)

echo floor(5.1);
//输出:5

echo floor(8.8);
//输出:8

进一法取整(向上取整)

echo ceil(5.1);
//输出:6

echo ceil(8.8);
//输出:9

普通四舍五入法

echo round(5.1);
//输出:5

echo round(8.8);
//输出:9

//保留两位小数并且进行四舍五入
echo round(5.123, 2);
//输出:5.12

echo round(8.888, 2);
//输出:8.89

//保留两位小数并且不进行四舍五入
echo substr(round(5.12345, 3), 0, -1);
//输出:5.12

echo substr(round(8.88888, 3), 0, -1);
//输出:8.88

银行家舍入法

四舍六入五考虑,五后非空就进一,五后为空看奇偶,五前为偶应舍去,五前为奇要进一。

保留两位小数,例:

  • 1.2849 = 1.28 -> 四舍
  • 1.2866 = 1.29 -> 六入
  • 1.2851 = 1.29 -> 五后非空就进一
  • 1.2850 = 1.28 -> 五后为空看奇偶,五前为偶应舍去
  • 1.2750 = 1.28 -> 五后为空看奇偶,五前为奇要进一

实现代码如下:

echo round(1.2849, 2, PHP_ROUND_HALF_EVEN);
//输出:1.28

echo round(1.2866, 2, PHP_ROUND_HALF_EVEN);
//输出:1.29

echo round(1.2851, 2, PHP_ROUND_HALF_EVEN);
//输出:1.29

echo round(1.2850, 2, PHP_ROUND_HALF_EVEN);
//输出:1.28

echo round(1.2750, 2, PHP_ROUND_HALF_EVEN);
//输出:1.28

 

数值格式化(千位分组)

应用于金额的展示,比如我们经常会看的银行卡余额。

echo number_format('10000.98', 2, '.', ',');
//输出:10,000.98

echo number_format('340888999', 2, '.', ',');
//输出:340,888,999.00

扩展

MySQL 浮点型字段

在 MySQL 中,创建表字段时也有浮点数类型。

浮点数类型包括单精度浮点数(float)和双精度浮点数(double)。

同理,不建议使用浮点数类型!!!

浮点数存在误差,当我们使用精度敏感的数据时,应该使用定点数(decimal)进行存储。