utecca/number
Composer 安装命令:
composer require utecca/number
包简介
A package for working with arbitrary precision numbers in Laravel.
README 文档
README
This package provides a number class that can be used to represent numbers.
All operations are done using bcmath which means that arbitrary precision is supported.
Installation
composer require utecca/number
Usage
// Init via constructor new Number(123.456); // Init via static method Number::of('123.45'); // Force a certain number of decimals Number::of('123.45', 1); // Will return 123.5 // Init from a value in cents Number::fromCents(12345); // Will return 123.45
Laravel casting
You can have models cast to Number by adding the following to your model:
protected $casts = [ // Amount with two decimals (usually used for monetary values) 'amount' => Utecca\Number\Casts\NumberFromDecimal::class, // Amount with a custom number of decimals 'quantity' => Utecca\Number\Casts\NumberFromDecimal::class . ':4'), ];
Basic operations
All operations will return the result as a new Number.
When doing operations, you always have the option to specify the number of decimals to use.
If not specified, the max number of decimals will be taken from the first operand's max.
// Addition $number->add(100); // Subtraction $number->sub(100); // Multiplication $number->mul(100); // Division $number->div(100); // Percentage $number->percent(50); // Round $number->round(2); // Floor $number->floor(); // Ceil $number->ceil(); // Absolute $number->abs();
Special operations
When doing special operations, you sometimes have the option to specify the number of decimals to use.
If not specified, the max number of decimals will be taken from the first operand's max.
All operations will return the result as a new Number.
// Extracts and returns the absolute decimal part of the number, eg. 123.45 wil return 0.45 Number::of(123.45)->decimalFraction(); // Returns 0.45 // Conditional negation Number::of(123.45)->negate(); // Returns -123.45 Number::of(123.45)->negate(false); // Returns 123.45 // Exchange rate conversion Number::of(123.45)->exchangeWithRate(745.5); // Returns 920.31 // Min Number::min([ Number::of(123.45), Number::of(100), ]); // Returns 100.00 // Max Number::max([ Number::of(123.45), Number::of(100), ]); // Returns 123.45
Other methods
// Various getters $number->isZero(); $number->isPositive(); $number->isPositiveOrZero(); $number->isNegative(); $number->isNegativeOrZero(); $number->lt(100); $number->lte(100); $number->gt(100); $number->gte(100); $number->eq(100); // Various formatters $number->toString(); $number->toInt(); $number->inCents(); // Only works if the number has two decimals or less
utecca/number 适用场景与选型建议
utecca/number 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.1k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 07 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「number」 「laravel」 「arbitrary」 「precision」 「bcmath」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 utecca/number 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 utecca/number 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 utecca/number 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
gmp-based arbitrary precision operations on currency amounts "XXX.YY"; because floats are BAD for representing money
Safe number conversion between arbitrary bases.
Gambling Algorithms for Certification.
Validates Turkish National Identification Number
Convert number (string format) between base 2 and 62
An arbitrary-precision Decimal class type for PHP.
统计信息
- 总下载量: 10.1k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-07-12