tetthys/bc
Composer 安装命令:
composer require tetthys/bc
包简介
Bc is a small and simple but accurate tool for calculation.
关键字:
README 文档
README
- System Requirements
- Bc
- Why Bc?
- Usage Examples
- About
nummethod - About
scalemethod - Supported Calculation Methods
- Supported Comparison Methods
- Runtime Exceptions
- How to contribute and test in same environment?
- What I want to say
System Requirements
- php 8.1 or later
- php bcmath extension
Bc
Bc is a small and simple but accurate tool for calculation. It uses bcmath functions internally.
You can get the exact result you want.
There are no rounding issues if you have enough memory.
It's like calculating to N decimal places using a pen and paper with your hands.
Why Bc?
Do you think below test will pass?
it('shows that 0.1 + 0.2 = 0.3', function () { expect(0.1 + 0.2)->toBe(0.3); });
Unfortunately, 0.1 + 0.2 is not 0.3 in php
It fails for the following reason:
Failed asserting that 0.30000000000000004 is identical to 0.3.
That's why I made this.
Usage Examples
Calculation
After giving a scale value, you can calculate in order.
(new Bc)->scale(2)->num('1')->add('2')->mul('3')->value(); // '9.00'
It also can be used like below:
(new Bc('1'))->add(new Bc('2'))->mul(new Bc('3'))->value(); // '9'
(new Bc)->scale(2)->num((new Bc('1')))->add(new Bc('2'))->mul(new Bc('3'))->value(); // '9.00'
Comparison
After giving a scale value, you can compare.
// true for '10.00' > '1.00' (new Bc)->scale(2)->num('10') ->isGreaterThan('1');
It also can be used like below:
// true for '30.00' > '3.00' (new Bc)->scale(2)->num('10')->add('20') ->isGreaterThan((new Bc)->scale(2)->num('1')->add('2'));
About num method
It specifies a number at which the calculation begins. It always returns Bc instance.
(new Bc)->num('1')
Or you can use the below instead:
(new Bc('1'))
About scale method
It specifies a scale value to be passed on to a next operation. The default value is 0.
(new Bc)->num('1')->add('2')->value(); // '3'
It supports chaining:
// With scale 0, '1' + '2' = '3' // With scale 2, '3.00' * '3.00' = '9.00' (new Bc)->num('1')->add('2')->scale(2)->mul('3')->value(); // '9.00'
Supported Calculation Methods
Calculation methods expect string or Bc instance. And they always return Bc instance.
add
It adds a number
(new Bc)->num('1')->add('2')->value(); // '3'
sub
It subtracts a number
(new Bc)->num('2')->sub('1')->value(); // '1'
mul
It multiplies a number
(new Bc)->num('2')->mul('3')->value(); // '6'
div
It divides a number
(new Bc)->num('6')->div('3')->value(); // '2'
mod
It calculates the modulus of a number
(new Bc)->num('10')->mod('7')->value(); // '3'
pow
It calculates the power of a number
(new Bc)->num('2')->pow('3')->value(); // '8'
powmod
It calculates the power of a number with modulus
(new Bc)->num('2')->powmod('5', '3')->value(); // '2'
sqrt
It calculates the square root of a number
(new Bc)->num('9')->sqrt()->value(); // '3'
Supported Comparison Methods
Comparison methods expect string or Bc instance. And they always return bool.
isGreaterThan
It checks if a number is greater than another number
(new Bc)->num('10')->isGreaterThan('1'); // true
isGreaterThanOrEqual
It checks if a number is greater than or equal to another number
(new Bc)->num('10')->isGreaterThanOrEqual('10'); // true
isLessThan
It checks if a number is less than another number
(new Bc)->num('1')->isLessThan('10'); // true
isLessThanOrEqual
It checks if a number is less than or equal to another number
(new Bc)->num('10')->isLessThanOrEqual('10'); // true
isEqual
It checks if a number is equal to another number
(new Bc)->num('10')->isEqual('10'); // true
isDifferent
It checks if a number is different from another number
(new Bc)->num('10')->isDifferent('1'); // true
gt
Same as isGreaterThan
(new Bc)->num('10')->gt('1'); // true
gte
Same as isGreaterThanOrEqual
(new Bc)->num('10')->gte('10'); // true
lt
Same as isLessThan
(new Bc)->num('1')->lt('10'); // true
lte
Same as isLessThanOrEqual
(new Bc)->num('10')->lte('10'); // true
is
Same as isEqual
(new Bc)->num('10')->is('10'); // true
isNot
Same as isDifferent
(new Bc)->num('10')->isNot('1'); // true
Runtime Exceptions
ScaleCannotBeUsedForOperation
throw new \Tetthys\Bc\Exceptions\ScaleCannotBeUsedForOperation;
This is thrown when scale is less than 0.
ValueCannotBeUsedForOperation
throw new \Tetthys\Bc\Exceptions\ValueCannotBeUsedForOperation;
This is thrown when value is not a number.
How to contribute and test in same environment?
docker-compose up
docker-compose up
attach shell to phptestenv container
bash ./run/shell/phptestenv.sh
install dependencies with composer
composer install
run test
./vendor/bin/pest
Or, you can run test from host OS using
bash ./run/test.sh
What I want to say
It is currently June 2024.
I'm still learning commit and branch naming conventions and php composer rules.
I hope you understand even if some of the names are bad.
Thank you.
tetthys/bc 适用场景与选型建议
tetthys/bc 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 56 次下载、GitHub Stars 达 5, 最近一次更新时间为 2024 年 06 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「decimal」 「math」 「calculation」 「number」 「arithmetic」 「precision」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tetthys/bc 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tetthys/bc 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tetthys/bc 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LaTeX formula to PNG
Field for number with restricted count of digits and decimal places
Calculation of the nearest distance to the seas: Azov, Baltic, Caspian and Black
PHP implementation of python's struct module.
EU VAT calculation, the way it should be. Revived fork of spaze/vat-calculator which was a standalone & modernized fork of mpociot/vat-calculator, with some new features.
Partition problem for balanced arrays splitting made easy.
统计信息
- 总下载量: 56
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-06-07