PHPackages                             jamal/unit-converter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jamal/unit-converter

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jamal/unit-converter
====================

A modern, well-tested unit conversion library for PHP 8.1+ with Laravel 10 integration.

2.0.2(7mo ago)017MITPHPPHP ^8.1CI failing

Since Nov 7Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/jamalnasir/unitconverter)[ Packagist](https://packagist.org/packages/jamal/unit-converter)[ RSS](/packages/jamal-unit-converter/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (5)Versions (5)Used By (0)

Unit Converter
==============

[](#unit-converter)

A modern, well‑tested unit conversion library for PHP 8.1+ with first‑class Laravel 10 integration.

Requirements
------------

[](#requirements)

- PHP: ^8.1
- Laravel: ^10.0 (for the Laravel integration)

Installation
------------

[](#installation)

Install via Composer:

```
composer require jamal/unit-converter

```

Laravel package auto‑discovery will register the service provider and facade automatically.

Optionally publish the config:

```
php artisan vendor:publish --tag=unit-converter-config

```

Quick start
-----------

[](#quick-start)

- Convert with the fluent Converter API (enums):

```
use Jamal\UnitConverter\Converter;
use Jamal\UnitConverter\Units\Length;

$converter = new Converter();
$feet = $converter->from(Length::meter, 10)->to(Length::foot); // 32.8084...
```

- Convert with the global helper:

```
$inches = unit_convert(2.5, 'length.meter', 'length.inch');
```

- Value object conversions:

```
use Jamal\UnitConverter\Measurement;
use Jamal\UnitConverter\Units\Temperature;

$m = new Measurement(100, Temperature::celsius);
$asF = $m->to(Temperature::fahrenheit); // Measurement(amount: 212, unit: fahrenheit)
```

- Laravel Facade:

```
use Jamal\UnitConverter\Laravel\UnitConverter as LC; // or alias "UnitConverter"

$mph = app('unit-converter')->from(\Jamal\UnitConverter\Units\Speed::kilometer_per_hour, 100)
    ->to(\Jamal\UnitConverter\Units\Speed::mile_per_hour);
```

Supported categories and units
------------------------------

[](#supported-categories-and-units)

- Length: meter, kilometer, centimeter, millimeter, micrometer, nanometer, mile, yard, foot, inch, nautical\_mile
- Mass: kilogram, gram, milligram, microgram, ton, pound, ounce
- Temperature: celsius, fahrenheit, kelvin
- Volume: liter, milliliter, cubic\_meter, cubic\_centimeter, cubic\_inch, cubic\_foot, US\_gallon, US\_quart, US\_pint, US\_fluid\_ounce, imperial\_gallon, imperial\_pint, imperial\_fluid\_ounce
- Area: square\_meter, square\_kilometer, square\_centimeter, square\_inch, square\_foot, acre, hectare
- Speed: meter\_per\_second, kilometer\_per\_hour, mile\_per\_hour, knot
- Time: second, millisecond, microsecond, minute, hour, day, week
- Digital Storage: bit, kilobit, megabit, gigabit, terabit, byte, kB, MB, GB, TB, KiB, MiB, GiB, TiB
- Pressure: pascal, kilopascal, bar, millibar, psi, atmosphere
- Energy: joule, kilojoule, calorie, kilocalorie, watt\_hour, kilowatt\_hour, btu
- Power: watt, kilowatt, horsepower
- Frequency: hertz, kilohertz, megahertz, gigahertz
- Fuel Economy: liter\_per\_100km, mpg\_US, mpg\_UK

Notes:

- Temperature and fuel economy use exact formulas (not factors).
- High precision arithmetic will use bcmath if the extension is available (configurable).

Configuration
-------------

[](#configuration)

Publish config creates config/unit-converter.php with:

- precision (default 9)
- rounding mode (PHP\_ROUND\_HALF\_UP)
- prefer\_bcmath (bool)
- decimal\_separator, thousands\_separator for formatting

Validation and Eloquent cast
----------------------------

[](#validation-and-eloquent-cast)

- Validation rule:

```
use Jamal\UnitConverter\Rules\ValidUnit;
$request->validate([
    'from' => [new ValidUnit('length')],
    'to'   => [new ValidUnit('length')],
]);
```

- Eloquent cast (stores value and unit code in separate columns):

```
use Jamal\UnitConverter\Casts\AsMeasurement;
use Jamal\UnitConverter\Units\Mass;

protected $casts = [
    'weight' => AsMeasurement::class,
];

// When saving
$model->weight = new \Jamal\UnitConverter\Measurement(72.5, Mass::kilogram);
// Will also set weight_unit = "mass.kilogram"
```

Extensibility
-------------

[](#extensibility)

You can register custom categories and units at runtime via UnitRegistry.

Upgrading from v1
-----------------

[](#upgrading-from-v1)

- PHP and Laravel requirements have changed to PHP 8.1+ and Laravel 10.
- API now uses enums and a Converter service. The old UC::kilometers()-&gt;toMile() style is deprecated.
- See UPGRADE.md for migration details (coming with the next release).

License
-------

[](#license)

This project is licensed under the MIT License - see the LICENSE file for details.

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance62

Regular maintenance activity

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~835 days

Total

4

Last Release

238d ago

Major Versions

1.0.0 → 2.0.02020-02-23

PHP version history (2 changes)1.0.0PHP &gt;=5.5.9

2.0.1PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/db863171f0f4e8b05e333bf1d9439e5869f084fe2a5c0c3df146bbe56d2fe57c?d=identicon)[jamalnasir](/maintainers/jamalnasir)

---

Top Contributors

[![jamalnasir](https://avatars.githubusercontent.com/u/1989782?v=4)](https://github.com/jamalnasir "jamalnasir (11 commits)")

---

Tags

laravelconversionunitsmeasurementunit-converter

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jamal-unit-converter/health.svg)

```
[![Health](https://phpackages.com/badges/jamal-unit-converter/health.svg)](https://phpackages.com/packages/jamal-unit-converter)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[danielme85/laravel-cconverter

Laravel 5 plug-in for currency conversion

42101.1k](/packages/danielme85-laravel-cconverter)[interaction-design-foundation/laravel-geoip

Support for multiple Geographical Location services.

17221.0k3](/packages/interaction-design-foundation-laravel-geoip)[nedwors/navigator

A Laravel package to ease defining navigation menus

433.1k](/packages/nedwors-navigator)[dcblogdev/laravel-junie

Install pre-configured guides for Jetbrains Junie

392.5k](/packages/dcblogdev-laravel-junie)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
