rtlopez/decimal
Composer 安装命令:
composer require rtlopez/decimal
包简介
An object oriented immutable arbitrary-precision arithmetic library for PHP
关键字:
README 文档
README
An object oriented arbitrary precision arithmetic class for PHP (>5.3.3).
Installation
You can use Composer
To add this package as a local, per-project dependency to your project, simply add a dependency on rtlopez/decimal to your project's composer.json file. Here is a minimal example of a composer.json file that just defines a dependency on Decimal:
{
"require": {
"rtlopez/decimal": "^1.0"
}
}
Features
The Decimal class can be used for an arbitrary precision calculation. Main features are:
- Immutable result of each operation,
- The same result precision,
- Safe for finance calculations,
- Fluent interface,
- Many unit test (more than 1100 tests and 3700 assertions),
- Easy to use and install,
Why should I use it?
The Decimal solve several inconveniences known in other solutions.
Floatis only an aproximation and have limited precision,Fixed-pointhave limited size (64-bits on 64-bit system),- Pure
bcmathcannot correctly round, only truncate, GMPis not object-oriented
Usage
Creating objects with simple factory
use RtLopez\Decimal; $n1 = Decimal::create(123, 2); $n2 = Decimal::create(123.45, 2); // not recommended $n3 = Decimal::create('123.45', 2); $n4 = Decimal::create($n1, 2);
Printing and formatting numbers
$number = Decimal::create('12345.671', 2); echo (string)$number; // 12345.67 echo $number->format(1, ',', ' '); // 12 345,7 echo $number->truncate()->format(null, ',', ' ', false); // 12 345,00
Arithmetics
$n1 = Decimal::create('12345.671', 3); $n2 = Decimal::create('11111.111', 3); $n3 = $n1->add($n2)->mul(-1); echo $n3; // -23456.782 // fluid interface $n4 = $n3->pow(2)->sqrt()->mul(-2)->div(2)->abs();
Immutable
$n1 = Decimal::create('12345.671', 3); $n1->add(2); ... echo $n1; // 12345.671
Comparisions
$n1 = Decimal::create('12345.671', 3); $n2 = Decimal::create('11111.111', 3); $n1->gt($n2); // true $n1->eq(0); // false $n2->le(100); // false
Rounding
$n = Decimal::create('12345.671', 3); echo $n->round(2); // 12345.67 echo $n->round(1); // 12345.7 echo $n->truncate(); // 12345 echo $n->ceil(1); // 12345.6 echo $n->floor(); // 12345
Implementations
There are exists three internal implementations of this library.
RtLopez\DecimalBCMath: (default)based on bcmath libraryRtLopez\DecimalFixed: based on scaled integersRtLopez\DecimalFloat: based on floats
Only BCMath implementation works correctly in wide spectrum of numbers. All others can fail in many border cases. They was implemented only as proof of concept and you can using it only for your own risk.
The default implementation or precision can be changed by functions:
Decimal::setDefaultImplementation('RtLopez\DecimalBCMath'); Decimal::setDefaultPrecision(6);
Licence
This library is distributed under MIT Licence.
Issue reporting or feature request
Coming soon.
TODO
- GMP implementation
Donation
BTC: 1Lopez7yPtbyjfLGe892JfheDFJMMt43tW LTC: LV3G3sJxz9AYpDMYUp8e1LCmerFYxVY3ak
rtlopez/decimal 适用场景与选型建议
rtlopez/decimal 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 272.61k 次下载、GitHub Stars 达 27, 最近一次更新时间为 2014 年 04 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「money」 「decimal」 「numbers」 「gmp」 「bignum」 「bcmath」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rtlopez/decimal 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rtlopez/decimal 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rtlopez/decimal 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Beyonic PHP Library
Field for number with restricted count of digits and decimal places
PHP implementation of python's struct module.
Partition problem for balanced arrays splitting made easy.
Easy to use arithmetic operations for decimals in PHP (using BCMath)
Rutils Symfony 2 bundle
统计信息
- 总下载量: 272.61k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 27
- 点击次数: 39
- 依赖项目数: 2
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2014-04-03