timg/decimal
Composer 安装命令:
composer require timg/decimal
包简介
README 文档
README
Decimal
PHP is very popular for developing business applications. Often such applications needs to calculate numbers, for example when dealing with money.
Even if php is a high level programming language, dealing with something like money is error prone and often not proper implemented.
One of the reasons for this is that php doesn't have a Decimal type. Often people use float's to do such calculations.
If you take a look at the floating point documentation there is a huge warning
So never trust floating number results to the last digit, and do not compare floating point numbers directly for equality.
It's quite easy to do this wrong and even if this just affect the last digit this could result in a huge, hard to fix problem.
If you simply want to calculate a price, or do some basic math you probably don't want to think about Floating-Point Arithmetic at all.
Decimal Type
The package comes with the classes Decimal and Decimal[0-100] the 0-100 indicates the precision.
This would allow you to write Something like (new Decimal2("1.99"))->add(new Decimal2("1.99")).
There are also helper functions to create Decimals: dec2("1.99")->add(dec2("1.99")). The Function dec2 returns a Decimal2, dec99 could return an Instance of Decimal99.
Rational
The package also comes with a Rational Type.
This could help you for example if you need to calculate something like 3 * (1/3). Using the rational type you could simply write
Rational::fromDecimal(dec0(3))->mul(new Rational(dec0(1), dec0(3)). There is also a rat() that looks a bit nicer.
Lazy Calculation
you could represent 1 + 2 as 3 but for more complex numbers like the Rational-Type this doesnt work well. For example you could not represent 1/3 as decimal without losing precision.
The Idea behind Lazy Calculation is, that instead of calculating numbers directly, just remember the calculation steps and calculate them as late as possible.
let's take a small look at this example:
$calculation = lazy_calc(dec(0)) ->add(lazy_calc(rat(1, 3))->round(2)) ->add(lazy_calc(rat(1, 3))->round(2)) ->add(lazy_calc(rat(1, 3))->round(3)); static::assertSame( '(((0 + round((1 / 3), scale = 2)) + round((1 / 3), scale = 2)) + round((1 / 3), scale = 3))', $calculation->pretty() );
As you can see here, $calculation just remembers the complete calculation. You can now do different things with the calculation, for example just print it down or use different calculators to calculate a result.
It is also possible to add hints to numbers and calculation steps and pretty-print the calculation with annotations.
This makes it really easy to understand huge calculations. For example this could be very interesting if you have to calculate a basket with a lot of different items, rebates, conditions and so on. Debugging the basket would be a lot easier if you could simply dump all calculation steps, annotated and explained.
timg/decimal 适用场景与选型建议
timg/decimal 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.07k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2018 年 03 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 timg/decimal 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 timg/decimal 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5.07k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-03-28