jimeneztdavid/scaled-int-laravel 问题修复 & 功能扩展

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

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

jimeneztdavid/scaled-int-laravel

Composer 安装命令:

composer require jimeneztdavid/scaled-int-laravel

包简介

Laravel integration for jimeneztdavid/scaled-int.

README 文档

README

Laravel integration for jimeneztdavid/scaled-int.

Why this package exists

ScaledInt solves a common problem in money and decimal-based attributes: databases store exact integers, while applications often need values such as 10.25, 2.750, or similar scaled numbers.

This package bridges that gap for Laravel. It lets you work with readable major values in your models while storing the minor integer representation in the database. That keeps arithmetic exact, avoids floating-point errors, and keeps Eloquent attributes simple to use.

The package also provides an Artisan command to add the cast to a model automatically.

Installation

composer require jimeneztdavid/scaled-int-laravel

Why this package exists in practice

Float math is not reliable for exact values

var_dump(0.1 + 0.2); // float(0.30000000000000004)
var_dump((0.1 + 0.2) === 0.3); // bool(false)

That is fine for approximate scientific calculations, but not for money, discounts, taxes, or any value that must round exactly.

Storing money as a float creates long-term problems

If you store a price as a float, you are asking the database and PHP to preserve decimal precision that binary floats do not represent cleanly.

Common Laravel workaround:

public function setPriceAttribute($value): void
{
    $this->attributes['price'] = (int) round($value * 100);
}

public function getPriceAttribute($value): float
{
    return $value / 100;
}

This works, but it means every model repeats the same logic. It also pushes rounding decisions into many places instead of one.

Comparisons with floats can be misleading

var_dump(0.3 === (0.1 + 0.2)); // bool(false)

With scaled integers, the comparison is exact because the stored value is an integer.

use Jimeneztdavid\ScaledInt\ScaledInt;

$a = ScaledInt::fromMajor('0.10');
$b = ScaledInt::fromMajor('0.20');

echo $a->add($b); // 0.30

The same problem appears when saving and loading values

Many projects end up doing this manually:

$product->price = 10.25;
$product->save();

// save as 1025
// read back as 10.25

That means multiplying by 100 on write, dividing by 100 on read, and keeping the column as an integer. ScaledIntCast does that for you in one place.

use Jimeneztdavid\ScaledIntLaravel\ScaledIntCast;

protected function casts(): array
{
    return [
        'price' => ScaledIntCast::class.':100',
    ];
}

What you get with ScaledInt

  • exact integer-based math
  • consistent rounding
  • easier comparisons
  • less custom accessor/mutator code
  • clearer intent in your models and services

Usage

Model cast

use Jimeneztdavid\ScaledIntLaravel\ScaledIntCast;

protected function casts(): array
{
    return [
        'price' => ScaledIntCast::class.':100',
    ];
}

What the cast does

  • stores the minor integer in the database
  • returns a Jimeneztdavid\ScaledInt\ScaledInt instance when reading the attribute
  • accepts ScaledInt, major numeric strings, integer minor values, and null

Example with data

$product = new Product();
$product->price = '10.25';
$product->save();

// database value: 1025
echo $product->price; // 10.25

ScaledInt methods

The jimeneztdavid/scaled-int package provides a value object with safe decimal arithmetic.

Creating values

use Jimeneztdavid\ScaledInt\ScaledInt;

$price = ScaledInt::fromMajor('10.25');
$weight = ScaledInt::fromMajor('2.750', 1000);
$minor = ScaledInt::fromMinor(1025);

echo $price; // 10.25
echo $weight; // 2.750
echo $minor; // 10.25

Reading values

  • minor() returns the stored integer value
  • scale() returns the scale used by the value
  • toMajorString() returns the human-readable decimal string
  • casting the object to string returns the same major value
echo $price->minor(); // 1025
echo $price->scale(); // 100
echo $price->toMajorString(); // 10.25
echo $price; // 10.25

Arithmetic

$price = ScaledInt::fromMajor('100.00');
$tax = ScaledInt::fromMajor('19.00');
$discount = ScaledInt::fromMajor('15.00');

$price->add($tax); // 119.00
$price->subtract($discount); // 85.00
$price->multiplyByInt(2); // 200.00
$price->divideByIntExact(2); // 50.00
$price->divideByInt(3, \Jimeneztdavid\ScaledInt\RoundingMode::HALF_UP); // 33.33
$price->percentOf(19, \Jimeneztdavid\ScaledInt\RoundingMode::HALF_UP); // 19.00

Comparisons

$price = ScaledInt::fromMajor('10.25');
$other = ScaledInt::fromMajor('10.30');
$min = ScaledInt::fromMajor('10.00');
$max = ScaledInt::fromMajor('20.00');

$price->compareTo($other); // -1
$other->compareTo($price); // 1
$price->min($other); // 10.25
$price->max($other); // 10.30
$price->between($min, $max); // true

Accepted assignment values

  • Jimeneztdavid\ScaledInt\ScaledInt instance, for example ScaledInt::fromMajor('10.25')
  • major numeric strings, such as '10.25', stored as 1025
  • integer minor values, such as 1025, returned as 10.25
  • null, stored as null

Supported usage patterns

$product->price = '10.25'; // stored as 1025
$product->price = \Jimeneztdavid\ScaledInt\ScaledInt::fromMajor('10.25', 100); // stored as 1025
$product->price = 1025; // read back as 10.25
$product->price = null; // stored as null

Artisan command

php artisan make:scaled-int-cast Product price --scale=100

Options:

  • --scale=100
  • --force
  • --dry-run

The command supports both existing casts(): array methods and legacy $casts properties.

Configuration

php artisan vendor:publish --tag=scaled-int-config

Configuration options:

  • default_scale: fallback scale used when no scale is passed to ScaledIntCast
  • default_cast: default cast class for package configuration

jimeneztdavid/scaled-int-laravel 适用场景与选型建议

jimeneztdavid/scaled-int-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 06 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 jimeneztdavid/scaled-int-laravel 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-06-03