PHPackages                             phrity/util-numerics - 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. phrity/util-numerics

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

phrity/util-numerics
====================

Utility library for numerics. Float versions of ceil(), floor() and rand() with precision. Open minded numeric parser and formatter.

2.4.1(5mo ago)29.2k↓13.3%MITPHPPHP ^8.1CI passing

Since Mar 28Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/sirn-se/phrity-util-numerics)[ Packagist](https://packagist.org/packages/phrity/util-numerics)[ Docs](https://phrity.sirn.se/util-numerics)[ RSS](/packages/phrity-util-numerics/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (4)Versions (14)Used By (0)

[![Phrity Util Numerics](docs/logotype.png)](docs/logotype.png)

[![Build Status](https://github.com/sirn-se/phrity-util-numerics/actions/workflows/acceptance.yml/badge.svg)](https://github.com/sirn-se/phrity-util-numerics/actions)[![Coverage Status](https://camo.githubusercontent.com/2840e232703d58821a7a7347f93f8a8922d515745168969a0e0f8f9497410297/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f7369726e2d73652f7068726974792d7574696c2d6e756d65726963732f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/github/sirn-se/phrity-util-numerics?branch=main)

Numerics utility
================

[](#numerics-utility)

Utility library for numerics. Float versions of `ceil()`, `floor()` and `rand()` with precision. An open minded numeric parser, formatter, plus some additional functions.

Current version supports PHP `^8.0`.

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

[](#installation)

Install with [Composer](https://getcomposer.org/);

```
composer require phrity/util-numerics

```

The Numerics class
------------------

[](#the-numerics-class)

### Class synopsis

[](#class-synopsis)

```
Phrity\Util\Numerics {

    /* Methods */
    public __construct(int|null $precision = null, string|null $locale = null);
    public ceil(float $number, int|null $precision = null): float;
    public floor(float $number, int|null $precision = null): float;
    public round(float $number, int|null $precision = null): float;
    public mceil(float $number, float $multipleOf): float;
    public mfloor(float $number, float $multipleOf): float;
    public mround(float $number, float $multipleOf): float;
    public parse(int|float|string $numeric): float|null;
    public format(float $number, int|null $precision = null): string;
    public rand(float $min = 0, float[null $max = null, int|null $precision = null): float|null;
    public precision(float $number, bool $wide = false): int;
    public setLocale(string $locale): void;
}
```

### Constructor

[](#constructor)

The constructor accepts default precision and/or locale. Precision can also be negative to achive integer rounding.

```
$numerics = new Numerics(); // Default precision is 0
$numerics = new Numerics(3, 'en_US'); // Default precision is 3, default locale en-US
```

### Ceil method

[](#ceil-method)

Round fractions up, according to precision specifier. A precision of `0` corresponds to PHP `ceil()` function. Precision can also be negative.

```
// Precison specified on each call
$numerics = new Numerics();
$numerics->ceil(1234.5678,  2); // 1234.57
$numerics->ceil(1234.5678,  1); // 1234.60
$numerics->ceil(1234.5678,  0); // 1235.00
$numerics->ceil(1234.5678, -1); // 1240.00
$numerics->ceil(1234.5678, -2); // 1300.00

// Precison specified as default, override is possible
$numerics = new Numerics(2);
$numerics->ceil(1234.5678); // 1234.57
$numerics->ceil(1234.5678, 0); // 1235.00
```

### Floor method

[](#floor-method)

Round fractions down, according to precision specifier. A precision of `0` corresponds to PHP `floor()` function. Precision can also be negative.

```
// Precison specified on each call
$numerics = new Numerics();
$numerics->floor(1234.5678,  2); // 1234.56
$numerics->floor(1234.5678,  1); // 1234.50
$numerics->floor(1234.5678,  0); // 1234.00
$numerics->floor(1234.5678, -1); // 1230.00
$numerics->floor(1234.5678, -2); // 1200.00

// Precison specified as default, override is possible
$numerics = new Numerics(2);
$numerics->floor(1234.5678); // 1234.56
$numerics->floor(1234.5678, 0); // 1234.00
```

### Round method

[](#round-method)

Standard round, according to precision specifier. Precision can also be negative.

```
// Precison specified on each call
$numerics = new Numerics();
$numerics->round(1234.5678,  2); // 1234.57
$numerics->round(1234.5678,  1); // 1234.60
$numerics->round(1234.5678,  0); // 1235.00
$numerics->round(1234.5678, -1); // 1230.00
$numerics->round(1234.5678, -2); // 1200.00

// Precison specified as default, override is possible
$numerics = new Numerics(2);
$numerics->round(1234.5678); // 1234.57
$numerics->round(1234.5678, 0); // 1235.00
```

### Ceil multiple-of method

[](#ceil-multiple-of-method)

Round up, according to multiple-of specifier.

```
$numerics = new Numerics();
$numerics->mceil(123.123, 0.25); // 123.25
$numerics->mceil(123.123, 2.5); // 125
$numerics->mceil(123.123, 25); // 125
$numerics->mceil(123.123, 250); // 250
```

### Floor multiple-of method

[](#floor-multiple-of-method)

Round down, according to multiple-of specifier.

```
$numerics = new Numerics();
$numerics->mfloor(456.789, 0.25); // 456.75
$numerics->mfloor(456.789, 2.5); // 455
$numerics->mfloor(456.789, 25); // 450
$numerics->mfloor(456.789, 250); // 250
```

### Round multiple-of method

[](#round-multiple-of-method)

Round, according to multiple-of specifier.

```
$numerics = new Numerics();
$numerics->mround(456.789, 0.25); // 456.75
$numerics->mround(456.789, 2.5); // 457.5
$numerics->mround(456.789, 25); // 450
$numerics->mround(456.789, 250); // 500
```

### Parse method

[](#parse-method)

Numeric parser. Parses number by evaluating input rather than using locale or making explicit assumtions. Returns `float`, or `null` if provided input can not be parsed.

```
$numerics = new Numerics();

// Integer and float input
$numerics->parse(1234.56); // 1234.56
$numerics->parse(1234); // 1234.00

// String input
$numerics->parse('1234.56'); // 1234.56
$numerics->parse('1234,56'); // 1234.56
$numerics->parse('1 234.56'); // 1234.56
$numerics->parse('1 234,56'); // 1234.56
$numerics->parse('1,234.56'); // 1234.56
$numerics->parse('1.234,56'); // 1234.56

// Evaluated string input
$numerics->parse(' 1 234.56 '); // 1234.56
$numerics->parse('-1,234.56'); // -1234.56
$numerics->parse('+1.234,56'); // 1234.56
$numerics->parse('.56'); // 0.56
$numerics->parse(',56'); // 0.56
$numerics->parse('12.'); // 12.0
$numerics->parse('12,'); // 12.0

// Locale support for fringe cases
$numerics->setLocale('sv_SE');
$numerics->parse('123,456'); // 123.456
$numerics->setLocale('en_US');
$numerics->parse('123,456'); // 123456.0
```

### Format method

[](#format-method)

Numeric formatter. Formats numbers according to precision (rounding and padding) and locale. Precision can also be negative.

```
$numerics = new Numerics();

$numerics->setLocale('sv_SE');
$numerics->format(1234.5678, 2); // "1 234,56"
$numerics->format(1234, 2); // "1 234,00"
$numerics->format(1234.5678, 0); // "1 234"
$numerics->format(1234, -2); // "1 200"

$numerics->setLocale('en_US');
$numerics->format(1234.5678, 2); // "1,234.56"
$numerics->format(1234, 2); // "1,234.00"
$numerics->format(1234.5678, 0); // "1,234"
$numerics->format(1234, -2); // "1,200"
```

Using format with many decimals.

```
$big = 0.123456789123456789;
$numerics->format($big); // "0.123457" - same as below
$numerics->format($big, $numerics->precision($big)); // "0.123457" - php decimal precision
$numerics->format($big, $numerics->precision($big, true)); // "0.123456789123457" - without precision loss
$numerics->format($big, 50); // "0.12345678912345678379658409085095627233386039733887" - with precision loss
```

### Rand method

[](#rand-method)

Float random number with precision. Precision can also be negative. Returns `float`, or `null` if impossible to generate result.

```
// Precison specified on each call
$numerics = new Numerics();
$numerics->rand(0, 10); // 0.0 … 10.0
$numerics->rand(0, 100, 2); // 0.00 … 100.00
$numerics->rand(-100, 100, 4); // -100.0000 … 100.0000
$numerics->rand(0.01, 0.97, 4); // 0.0100 … 0.9700
$numerics->rand(9, 11, -1); // 10.0
$numerics->rand(90, 110, -2); // 100.0

// Precison specified as default, override is possible
$numerics = new Numerics(2);
$numerics->rand(0, 100); // 0.00 … 100.00
```

### Precision method

[](#precision-method)

Count number of relevant decimals in a number. By default the number of serializable decimals (as of php evaluation) is returned.

```
$numerics = new Numerics();
$numerics->precision(12); // 0
$numerics->precision(12.0); // 0
$numerics->precision(12.34); // 2
$numerics->precision(0.123456789123456789); // 6
```

By setting the `wide` option, number of decimals usable without precision loss is returned.

```
$numerics = new Numerics();
$numerics->precision(0.123456789123456789, true); // 15
```

### setLocale method

[](#setlocale-method)

Affects the `format()` method and fringe case in `parse()` method. If not set, current locale will be used in these methods.

```
$numerics = new Numerics();
$numerics->setLocale('sv_SE'); // Set to Swedish
```

Versions
--------

[](#versions)

VersionPHP`2.5``^8.1``2.4``^8.0`Multiple-of rounding: `mround()` `mfloor()` `mceil()` methods`2.3``^7.4|^8.0`Precision improvements, negative precision in format()`2.2``^7.4|^8.0`Default locale`2.1``^7.1|^8.0``2.0``^7.1`Instanceable, `format()` method, ability to specify locale`1.2``>=5.6``rand()` and `precision()` methods`1.1``>=5.6``parse()` method`1.0``>=5.6``ceil()` and `floor()` methods

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance71

Regular maintenance activity

Popularity26

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity76

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 ~203 days

Recently: every ~453 days

Total

13

Last Release

164d ago

Major Versions

1.2.1 → 2.0.02019-07-24

PHP version history (6 changes)1.0.0PHP &gt;=5.6

2.0.0PHP ^7.1

2.1.0PHP ^7.1|^8.0

2.2.0PHP ^7.4|^8.0

2.4.0PHP ^8.0

2.4.1PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/4255391?v=4)[Sören Jensen](/maintainers/sirn-se)[@sirn-se](https://github.com/sirn-se)

---

Top Contributors

[![sirn-se](https://avatars.githubusercontent.com/u/4255391?v=4)](https://github.com/sirn-se "sirn-se (45 commits)")

---

Tags

ceilfloorformatnumericsparsephpphp-libraryprecisionrandomformatparsenumbersfloatprecisionceilfloornumerics

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/phrity-util-numerics/health.svg)

```
[![Health](https://phpackages.com/badges/phrity-util-numerics/health.svg)](https://phpackages.com/packages/phrity-util-numerics)
```

###  Alternatives

[willdurand/negotiation

Content Negotiation tools for PHP provided as a standalone library.

1.4k122.0M157](/packages/willdurand-negotiation)[gabrielelana/byte-units

Library to parse, format and convert byte units

1672.2M19](/packages/gabrielelana-byte-units)[akaunting/laravel-money

Currency formatting and conversion package for Laravel

7825.3M18](/packages/akaunting-laravel-money)[kwn/number-to-words

Multi language standalone PHP number to words converter. Fully tested, open for extensions and new languages.

4235.0M21](/packages/kwn-number-to-words)[enqueue/dsn

Parse DSN

12525.5M45](/packages/enqueue-dsn)[gajus/dindent

HTML indentation library for development and testing.

1781.9M50](/packages/gajus-dindent)

PHPackages © 2026

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