I built a fluent Numberable API for Laravel (v1.0.0)
Source: Dev.to
Introduction
Laravel provides a Stringable class for string manipulation, but numeric logic in applications often remains messy.
laravel-numberable v1.0.0 is a small package that offers fluent numeric operations and formatting for Laravel.
Installation
composer require tresor-kasenda/laravel-numberable
Basic Usage
$price = number(1999.99)
->withLocale('en_US')
->withCurrency('USD');
$price->asCurrency(); // "$1,999.99"
Chaining Operations
$total = number(120)
->add(30)
->multiply(1.2)
->round(2);
Helper Methods
| Example | Result |
|---|---|
number(15)->clamp(0, 10); | 10 |
number(10.0)->trim(); | 10 |
number(17)->isPrime(); | true |
number(12)->isEven(); | true |
number(10)->between(5, 15); | true |
number(0.157)->asPercentage(); | 15.7% |
number(1532000)->asAbbreviated(); | 1.53M |
number(1048576)->asFileSize(); | 1 MB |
number(21)->asOrdinal(); | 21st |
Advanced Features
- Localized parsing:
from('1 234,50', 'fr_FR') - Conditional pipelines:
when()/unless() - Macros & custom formats
Compatibility
- Laravel 10 / 11 / 12
- PHP 8.3 / 8.4
Release
Version: v1.0.0
Repository
github.com/Tresor-Kasenda/laravel-numberable
Feedback
Feedback is welcome, especially regarding API naming and any missing numeric helpers.
Tags
- laravel
- php
- opensource
- webdev