非法/流畅-bcmath

流畅BCMath是一个PHP包,旨在以更流畅、更直观的方式简化PHP的bcmath函数的使用。凭借其表达性语法和Laravel兼容性,它旨在提高开发者在使用任意精度算术运算时的体验。

v1.0.1 2023-04-10 22:02 UTC

This package is auto-updated.

Last update: 2024-09-17 17:33:44 UTC


README

流畅BCMath是PHP BcMath扩展的流畅接口。

它可以帮助你编写更易读的代码,同时保持BcMath扩展的性能。

安装

通过composer安装包

composer require "illegal/fluent-bcmath"

使用方法

使用流畅接口有两种选择。

在两种情况下,都有两个参数:数字和比例。数字可以是

  • 一个字符串
  • 一个整数
  • 一个浮点数
  • 另一个 BCNumber 实例

比例是一个整数,表示小数点后的位数。

所有方法都返回一个新的 BCNumber 实例,因此你可以链式调用它们。

例如

use Illegal\FluentBCMath\BCNumber;
$num = new BCNumber('1.23', 2);

$num->add(2)->sub(2)->mul(2)->div(2)->mod(3)->pow(2)->sqrt();

1. 使用 BCNumber

use Illegal\FluentBCMath\BCNumber;

$num = new BCNumber('1.23', 2); // 1st argument is the number, 2nd argument is the scale

2. 使用 fnum() 辅助函数

$num = fnum('1.23', 2); // 1st argument is the number, 2nd argument is the scale

可用方法

$num = fnum(10, 2);

$num->add(2); // 12.00
$num->sub(2); // 8.00
$num->mul(2); // 20.00
$num->div(2); // 5.00
$num->mod(3); // 1.00
$num->pow(2); // 100.00
$num->sqrt(); // 3.16

$num->equals(10); // true
$num->greaterThan(5); // true
$num->greaterThanOrEqual(10); // true
$num->lessThan(15); // true
$num->lessThanOrEqual(10); // true
$num->isZero(); // false
$num->isPositive(); // true
$num->isNegative(); // false
$num->isEven(); // true
$num->isOdd(); // false
$num->abs(); // 10.00
$num->negate(); // -10.00
$num->min(5); // 5.00
$num->max(15); // 15.00
$num->clamp(5, 15); // 10.00

如果方法

每个操作都有一个 if 方法,它会在条件为真时返回操作的结果。

$num = fnum(10, 2);

$num->addIf(2, false); // 10.00
$num->subIf(2, false); // 10.00
$num->mulIf(2, false); // 10.00
$num->divIf(2, false); // 10.00
$num->modIf(3, false); // 10.00
$num->powIf(2, false); // 10.00
$num->sqrtIf(false); // 10.00

测试

./vendor/bin/pest

许可证

MIT许可证(MIT)。请参阅许可证文件获取更多信息。