andkom/php-decimal 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

andkom/php-decimal

Composer 安装命令:

composer require andkom/php-decimal

包简介

Arbitrary-precision decimal arithmetic library for PHP, built on top of bcmath.

README 文档

README

Arbitrary-precision decimal arithmetic library for PHP, built on top of the bcmath extension.

Provides both mutable (Decimal) and immutable (DecimalImmutable) implementations with a full set of arithmetic operations, rounding modes, comparisons, and formatting options.

Requirements

  • PHP 7.4+
  • bcmath extension

Installation

composer require andkom/php-decimal

Quick Start

use AndKom\Decimal;
use AndKom\DecimalImmutable;

// Create from various types
$a = new Decimal('1.5', 2);
$b = Decimal::create(2);
$c = Decimal::createFromFloat(3.14, 2);

// Arithmetic (mutable - modifies in place)
$result = (new Decimal('10', 2))->add('3.5')->multiply('2');
echo $result->getValue(); // '27.00'

// Immutable - always returns a new instance
$price = new DecimalImmutable('19.99', 2);
$discounted = $price->multiply('0.9');
echo $price->getValue();      // '19.99' (unchanged)
echo $discounted->getValue();  // '17.99'

// Rounding
echo (new Decimal('2.5', 1))->roundHalfUp()->getValue();   // '3'
echo (new Decimal('2.5', 1))->roundHalfEven()->getValue();  // '2'

// Formatting
echo (new Decimal('1234567.89', 2))->toFormat(2); // '1,234,567.89'
echo (new Decimal('1.5', 1))->toScientific(2);    // '1.5E0'
echo (new Decimal('1.234', 3))->toFixed(2);        // '1.23'

// Comparisons
$a = new Decimal('1.5', 1);
$a->isGreaterThan('1');    // true
$a->isEqual('1.5');        // true
$a->isPositive();          // true

Creating Decimals

Method Description
new Decimal($value, $scale) Constructor, value is a string, scale is optional
Decimal::create($value, $scale) Factory, auto-detects type (string, int, float, Decimal)
Decimal::createFromString($value, $scale) From string, supports scientific notation (e.g. '1.5e-3')
Decimal::createFromFloat($value, $scale) From float, auto-detects scale if not provided
Decimal::createFromInteger($value, $scale) From integer
Decimal::createFromDecimal($decimal, $scale) Clone from another Decimal

Default Scale

Decimal::setDefaultScale(4);
echo (new Decimal('1'))->getValue(); // '1.0000'
echo Decimal::getDefaultScale();     // 4

Arithmetic

All arithmetic methods accept an optional $scale parameter to override the result scale.

Method Description
add($value, $scale) Addition
subtract($value, $scale) Subtraction
multiply($value, $scale) Multiplication
divide($value, $scale) Division
modulus($value) Modulo (remainder)
power($value, $scale) Exponentiation (supports negative exponents)
squareRoot($scale) Square root
echo (new Decimal('10', 2))->divide('3', 4)->getValue(); // '3.3333'
echo (new Decimal('2'))->power(-3, 3)->getValue();        // '0.125'
echo (new Decimal('2'))->squareRoot(4)->getValue();        // '1.4142'

Rounding

All rounding methods accept an optional $precision parameter. Positive precision rounds decimal places, negative precision rounds to powers of 10.

Method Description
round($precision, $mode) Round with specified mode (default: ROUND_HALF_UP)
roundUp($precision) Round away from zero
roundDown($precision) Round toward zero (truncation)
roundHalfUp($precision) Round half away from zero
roundHalfDown($precision) Round half toward zero
roundHalfEven($precision) Round half to even (banker's rounding)
roundHalfOdd($precision) Round half to odd
floor($precision) Round toward negative infinity
ceil($precision) Round toward positive infinity
truncate($precision) Truncate toward zero

Rounding Mode Constants

Constant Value
ROUND_UP 1
ROUND_DOWN 2
ROUND_HALF_UP 3
ROUND_HALF_DOWN 4
ROUND_HALF_EVEN 5
ROUND_HALF_ODD 6
echo (new Decimal('2.5', 1))->roundHalfUp()->getValue();   // '3'
echo (new Decimal('2.5', 1))->roundHalfDown()->getValue();  // '2'
echo (new Decimal('2.5', 1))->roundHalfEven()->getValue();  // '2'
echo (new Decimal('3.5', 1))->roundHalfEven()->getValue();  // '4'
echo (new Decimal('-2.1', 1))->roundUp()->getValue();       // '-3'
echo (new Decimal('-2.1', 1))->ceil()->getValue();          // '-2'
echo (new Decimal('-2.1', 1))->floor()->getValue();         // '-3'
echo (new Decimal('123'))->round(-1)->getValue();           // '120'

Sign Operations

Method Description
inverse() Flip sign (positive becomes negative and vice versa)
negate() Always return negative value
absolutize() Always return positive value (absolute value)
echo (new Decimal('-5'))->inverse()->getValue();    // '5'
echo (new Decimal('5'))->inverse()->getValue();     // '-5'
echo (new Decimal('5'))->negate()->getValue();      // '-5'
echo (new Decimal('-5'))->negate()->getValue();     // '-5'
echo (new Decimal('-5'))->absolutize()->getValue(); // '5'

Conversion & Formatting

Method Description
toFloat() Convert to PHP float
toInt() Convert to PHP int (truncates decimal part)
toString($trailingZeros) String without trailing zeros (default), or with
toFixed($precision) String with exact decimal places (does not mutate)
toFormat($precision, $decPoint, $thousandsSep, $trailingZeros, $mode) Formatted number with thousands separator
toScientific($precision, $exponent) Scientific notation (e.g. '1.23E4')
toDigits($digits) Round to N significant digits
toDecimal() Clone as new instance of same type
toMutable() Convert to mutable Decimal
toImmutable() Convert to immutable DecimalImmutable
$d = new Decimal('1234.5678', 4);

echo $d->toFixed(2);               // '1234.56'
echo $d->toFormat(2);              // '1,234.57'
echo $d->toScientific(3);          // '1.234E3'
echo $d->toDigits(3)->getValue();  // '1230'
echo $d->toString();               // '1234.5678'
echo $d->toFloat();                // 1234.5678
echo $d->toInt();                  // 1234

Comparisons

All comparison methods accept an optional $scale parameter.

Method Description
compareTo($arg, $scale) Returns -1, 0, or 1
isEqual($arg, $scale) Equal to
isNotEqual($arg, $scale) Not equal to
isGreaterThan($arg, $scale) Greater than
isGreaterThanOrEquals($arg, $scale) Greater than or equal
isLessThan($arg, $scale) Less than
isLessThanOrEquals($arg, $scale) Less than or equal

Predicates

Method Description
isInteger() True if no fractional part
isDecimal() True if has fractional part
isPositive() True if greater than zero
isNegative() True if less than zero
isZero() True if equal to zero
isNotZero() True if not equal to zero
isEven() True if integer part is even
isOdd() True if integer part is odd

Static Helpers

Method Description
Decimal::max(...$args) Returns the maximum value
Decimal::min(...$args) Returns the minimum value
echo Decimal::max('1', '5', '3')->getValue(); // '5'
echo Decimal::min('1', '5', '3')->getValue(); // '1'

Mutable vs Immutable

Decimal is mutable -- operations modify the instance in place and return $this:

$d = new Decimal('10');
$d->add('5');
echo $d->getValue(); // '15' (modified)

DecimalImmutable always returns a new instance, leaving the original unchanged:

$d = new DecimalImmutable('10');
$result = $d->add('5');
echo $d->getValue();      // '10' (unchanged)
echo $result->getValue();  // '15' (new instance)

Convert between them with toMutable() and toImmutable().

Running Tests

composer install
vendor/bin/phpunit

andkom/php-decimal 适用场景与选型建议

andkom/php-decimal 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「decimal」 「math」 「bcmath」 「bigdecimal」 「Arbitrary-precision」 「bignumber」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 andkom/php-decimal 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 andkom/php-decimal 我们能提供哪些服务?
定制开发 / 二次开发

基于 andkom/php-decimal 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 26
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-08