louisgab/php-calc
Composer 安装命令:
composer require louisgab/php-calc
包简介
Simple fluent float manipulation library
README 文档
README
💯 Simple fluent float manipulation library.
Calc aims to provide tools for easy and readable calculations, without any dependency.
It comes with Number which is an immutable value object that enables fluent float manipulations.
Why
If you ever worked with a codebase full of that kind of crap:
$result = round(($b != 0 ? ((1+$a)/$b) : $c)*0.25, 2)
I'm sure you will enjoy that:
$result = Number::of($b) ->when(Number::of($b)->isZero(), fn($b) => $c fn($b) => Number::one()->plus($a)->divide($b), ) ->multiply(0.25) ->round(2) ->value()
You may think it's like brick/math, which is a really great package, but Calc serves a different purpose.
If floats are good enough for you - and unless you're dealing with sensible data like accounting or science, it should be - then using GMP or bcmath is overkill.
That's what Calc is made for, still using floats while enjoying nice readability.
Another good point is that it handles floating point problems (e.g 0.1 + 0.2 == 0.3 // false) as much as possible, so you don't have to think about it each time (and if you are working with junior developers, it will save them from having problems they didn't even know existed!).
Install
Via composer:
composer require louisgab/php-calc
Usage
Simple as:
use Louisgab\Calc\Number; Number::of($anything);
And good as :
public function carsNeeded(Number $people, Number $placesPerCar): int { return $people->divide($placesPerCar)->round(0)->toInt(); }
Please see DOCS
Testing
composer test
Roadmap
-
Number -
Fraction -
Percentage
Changelog
Please see CHANGELOG
Contributing
Highly welcomed!
License
Please see The MIT License (MIT).
统计信息
- 总下载量: 139
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 16
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-12-22