vrnedwidek / php-decimal
PHP 的任意精度十进制类。从 Piggly 分支出来,并更新了要求以支持 PHP 8.1 和 PHP 8.2。
Requires
- php: ^7.3 || ^8
- ext-bcmath: ^7.3 || ^8
Requires (Dev)
- phpunit/phpunit: ^9.5
README
此库与 decimal.js 类似,所有业务逻辑的版权和著作权归 Michael Mclaughlin 和您的 贡献者 所有。
piggly/php-decimal
与 PHP 完全兼容,速度与 decimal.js 库一样快。以下是与之相关的所有功能。
功能
此库主要是由 Decimal
类组成。一个 Decimal
由系数、指数和符号组成。它可以处理任意精度的整数和浮点数。
这里的精度是以有效数字来指定的,而不是小数位数,所有的计算都四舍五入到精度(类似于 Python 的 decimal 模块),而不是仅仅涉及除法。
此库还添加了三角函数等,并支持非整数幂,使其成为一个更大的库。
- 整数和浮点数;
- 简单但功能齐全的 API;
- 复制了许多原生数学方法;
- 也处理十六进制、二进制和八进制值;
- 比其他 PHP 库更快、更易于使用;
- 无依赖项或要求;
- 全面文档和测试套件。
使用
Decimal
可以处理整数、浮点数、字符串和 Decimal
对象
$w = new Decimal(123); $x = new Decimal(123.4567); $y = new Decimal('123456.7e-3'); $z = new Decimal($x);
如果包含适当的前缀,一个值也可以是二进制、十六进制或八进制
$w = new Decimal('0xff.f'); // '255.9375' $x = new Decimal('0b10101100'); // '172'
Decimal
是不可变的,意思是它的方法不会改变它,总是返回一个新的 object
。
$x = new Decimal(0.3); $x->minus(0.1); // $x is still 0.3 $y = $x->minus(0.1)->minus(0.1); // $x is still 0.3 and $y is 0.1
所有返回 Decimal
值的方法都可以链接。
$x->dividedBy($y)->plus($z)->times(9)->floor(); $x->times('1.23456780123456789e+9')->plus(9876.5432321)->dividedBy('4444562598.111772')->ceil();
许多方法名有简短的别名。
$x->squareRoot()->dividedBy($y)->toPower(3)->equals($x->sqrt()->div($y)->pow(3)) // true $x->cmp($y->mod($z)->neg()) == 1 && x->comparedTo($y->modulo($z)->negated()) == 1 // true
有许多方法可以将 Decimal
转换为字符串,
$x = new Decimal(255.5); $x->toExponential(5) // '2.55500e+2' $x->toFixed(5) // '255.50000' $x->toPrecision(5) // '255.50' $x->valueOf() // '255.5'
几乎所有的方法都作为 static
提供。
Decimal::sqrtOf('6.98372465832e+9823') // '8.3568682281821340204e+4911' Decimal::powOf(2, 0.0979843) // '1.0702770511687781839'
Decimal
可以处理 INF
和 NAN
值。
$x = new Decimal(INF); // INF $y = new Decimal(NAN); // NAN
有许多方法可以对 Decimal
进行任何检查。
$x->isCountless() // If it is INF or NAN $x->isFinite() // If it is a finite number $x->isInfinite() // If it is an infinite number $x->isInt() // If it is an integer $x->isNaN() // If it is NAN $x->isNegative() // If it is negative $x->isNulled() // If it is INF, NAN or Zero $x->isPositive() // If it is positive $x->isZero() // If it is zero
顺便说一下,有一个可选最大分母参数的 toFraction
方法。
$z = new Decimal(355); $pi = $z->dividedBy(113); // '3->1415929204' $pi->toFraction(); // [ '7853982301', '2500000000' ] $pi->toFraction(1000); // [ '355', '113' ]
所有计算都根据 DecimalConfig
对象的 precision
和 rounding
属性指定的有效数字数和舍入模式进行舍入。
每个 Decimal
类都与一个 DecimalConfig
关联。它可能是 global
配置,或者是对特定十进制数字的定制配置。
对于高级使用,可以创建多个 Decimal
,每个都有自己的独立配置,该配置适用于从它创建的所有 Decimal
数字。
// Set the precision and rounding of the global instance, // applies to all Decimal objects without configurations attached to it. DecimalConfig::instance()->set([ 'precision' => 5, 'rounding' => 4 ]); $decimal9 = DecimalConfig::clone()->set([ 'precision' => 9, 'rounding' => 1 ]); $x = new Decimal(5); $y = new Decimal(5, $decimal9); $x->div(3); // '1.6667' $y->div(3); // '1.66666666' // $decimal9 applies to all `Decimal` numbers // created from $y in this case $y->div(3)->times(1.5) // '2.50000000'
Decimal
对象的值以浮点格式存储,以它的 digits
、exponent
和 sign
为条件。
$x = new Decimal(-12345.67); $x->getDigits(); // [ 12345, 6700000 ] digits (base 10000000) $x->getExponent(); // 4 exponent (base 10) $x->getSign(); // -1 sign
有关更多信息,请参阅文档目录中的 API 参考,现在您可以访问 decimal.js API,因为此库与它完全兼容。
安装
Composer
- 在您的控制台,在您的项目文件夹中,键入
composer require piggly/php-decimal
; - 别忘了在您的代码库中添加 Composer 的自动加载文件
require_once('vendor/autoload.php');
。
手动安装
- 下载或通过以下命令克隆仓库:
git clone https://github.com/piggly-dev/php-decimal.git
; - 然后,进入:
cd /path/to/piggly/php-decimal
; - 使用
composer install
安装所有Composer依赖项; - 将项目的自动加载文件添加到您的代码库中:
require_once('/path/to/piggly/php-decimal/vendor/autoload.php');
。
依赖项
该库有以下外部依赖
- PHP 7.3+。
待办事项
在代码中,有一些带有@todo
注释的部分,表示该库可能需要的改进。
变更日志
有关所有代码变更的信息,请参阅变更日志文件。
代码测试
此库使用PHPUnit。我们对该应用程序的所有主要类进行测试。
vendor/bin/phpunit
您必须始终使用PHP 7.3或更高版本运行测试。此库的任何更改都需要通过所有最旧和最新的测试。
!! 一些测试很重,测试时要小心,它们可能需要大量可用内存。
贡献
在提交贡献之前,请参阅贡献文件中的信息。
致谢
decimal.js
支持项目
Piggly Studio是一家位于巴西里约热内卢的机构。如果您喜欢这个库并想支持这项工作,请随意向以下BTC钱包捐赠任何金额:3DNssbspq7dURaVQH6yBoYwW3PhsNs8dnK
❤。
许可协议
MIT许可协议(MIT)。请参阅许可协议。