承接 514sid/num 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

514sid/num

最新稳定版本:v2.1.0

Composer 安装命令:

composer require 514sid/num

包简介

Num

README 文档

README

Latest Stable Version Total Downloads License Tests

An accurate PHP helper for parsing numbers from strings with support for various thousands and decimal separators.

Hero

Requirements

Package Version PHP Version
v2.x ^8.1
v1.x ^8.0

Installation

$ composer require 514sid/num

What It Does

The built-in PHP functions intval() and floatval(), along with typecasting, may not always correctly handle varying numeric value formats based on regional standards.

floatval("1 234 567.89")    // float(1)
intval("1,234.56")          // int(1)

With the Num helper, you can achieve the desired functionality.

You have the option to provide the decimal separator to the int() or float() methods.

Alternatively, you can allow the Num helper to make an educated guess if you're unsure about the exact separator used in a specific string representing a numeric value.

use Num\Num;
use Num\Enums\DecimalSeparator;

Num::float('1,234,567.89', DecimalSeparator::POINT) // float(1234567.89)
Num::float('1.234.567,89', DecimalSeparator::COMMA) // float(1234567.89)
// or
Num::float('1,234,567.89')  // float(1234567.89)
Num::float('1.234.567,89')  // float(1234567.89)
Num::float(123)             // float(123.0)

Num::int('1,234,567.89')    // int(1234567)
Num::int('1.234.567,89')    // int(1234567)
Num::int(123.45)            // int(123)

Num::float('text')  // float(0.0)
Num::int('text')    // int(0)

Num::int('1.23e3')    	  // int(1230)
Num::float('-5.67e-4')    // float(-0.000567)

How It Works

When you pass a decimal separator as the second argument to the int() or float() static methods, they remove everything from the string except digits and the decimal separator, and then perform typecasting using PHP's built-in functionality.

If you do not specify a decimal separator, the Num helper tries to guess it using the DecimalSeparatorGuesser, which relies on formatting conventions from the Wikipedia article: https://en.wikipedia.org/wiki/Decimal_separator.

I am still working on improving DecimalSeparatorGuesser, so it might not be 100% accurate.

In most locales, for numbers smaller than 100000, a thousand separator is used based on powers of 1000.

This means that if a string is provided with only one dot or comma and there are 3 digits following it, the guesser will treat this number as a whole number; otherwise it will treat it as a float.

use Num\Num;

Num::float('12.34567')  // float(12.34567)
Num::float('12.34')     // float(12.34)
Num::float('12.345')    // float(12345.0)
Num::float('1234,567')  // float(1234.567)

Laravel Casts

use Num\Casts\NumInt;
use Num\Casts\NumFloat;

protected $casts = [
    'integer' => NumInt::class,
    'float'   => NumFloat::class,
];

License

MIT

514sid/num 适用场景与选型建议

514sid/num 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16.06k 次下载、GitHub Stars 达 17, 最近一次更新时间为 2023 年 08 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 514sid/num 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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