PHPackages                             data-values/number - 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. data-values/number

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

data-values/number
==================

Numerical value objects, parsers and formatters

0.13.0(1y ago)18303.0k↑11.8%6[1 issues](https://github.com/wmde/Number/issues)[3 PRs](https://github.com/wmde/Number/pulls)13GPL-2.0-or-laterPHPPHP &gt;=7.4

Since Nov 17Pushed 1y ago18 watchersCompare

[ Source](https://github.com/wmde/Number)[ Packagist](https://packagist.org/packages/data-values/number)[ Docs](https://github.com/DataValues/Number)[ RSS](/packages/data-values-number/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (32)Used By (13)

DataValues Number
=================

[](#datavalues-number)

Library containing value objects to represent numeric information, parsers to turn user input into such value objects, and formatters to turn them back into user consumable representations.

It is part of the [DataValues set of libraries](https://github.com/DataValues).

[![Build Status](https://github.com/wmde/Number/actions/workflows/lint-and-test.yaml/badge.svg?branch=master)](https://github.com/wmde/Number/actions/workflows/lint-and-test.yaml)[![Code Coverage](https://camo.githubusercontent.com/9140315ed26479f944ef59ac72adb57b63e24bd383605349debf4f09642bb24e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f776d64652f4e756d6265722f6261646765732f636f7665726167652e706e673f733d61363264643835643035656166306335353035646565643465326264353364333465353064313538)](https://scrutinizer-ci.com/g/wmde/Number/)[![Scrutinizer Quality Score](https://camo.githubusercontent.com/699211af311170dc080b4b74e9961acd629241e3c582186e9f2d3e715d9e61bf/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f776d64652f4e756d6265722f6261646765732f7175616c6974792d73636f72652e706e673f733d30333237393533306661353534333964653363653039346239383566383631393539656537313632)](https://scrutinizer-ci.com/g/wmde/Number/)

On [Packagist](https://packagist.org/packages/data-values/number): [![Latest Stable Version](https://camo.githubusercontent.com/374e6563d11c1317c609a6fb169d54e774d61a84cc9b2a8b99127cc9857d9cc3/68747470733a2f2f706f7365722e707567782e6f72672f646174612d76616c7565732f6e756d6265722f76657273696f6e2e706e67)](https://packagist.org/packages/data-values/number)[![Download count](https://camo.githubusercontent.com/07c7acaf217315409e746cb07e0c1a8543923ced4745ff7ae98ca43549a16ea4/68747470733a2f2f706f7365722e707567782e6f72672f646174612d76616c7565732f6e756d6265722f642f746f74616c2e706e67)](https://packagist.org/packages/data-values/number)

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

[](#installation)

The recommended way to use this library is via [Composer](http://getcomposer.org/).

### Composer

[](#composer)

To add this package as a local, per-project dependency to your project, simply add a dependency on `data-values/number` to your project's `composer.json` file. Here is a minimal example of a `composer.json` file that just defines a dependency on version 0.8 of this package:

```
{
    "require": {
        "data-values/number": "0.12.*"
    }
}

```

### Manual

[](#manual)

Get the code of this package, either via git, or some other means. Also get all dependencies. You can find a list of the dependencies in the "require" section of the composer.json file. Then take care of autoloading the classes defined in the src directory.

Tests
-----

[](#tests)

This library comes with a set up PHPUnit tests that cover all non-trivial code. You can run these tests using the PHPUnit configuration file found in the root directory. The tests can also be run via TravisCI, as a TravisCI configuration file is also provided in the root directory.

Authors
-------

[](#authors)

DataValues Number was created by [Wikimedia Deutschland](https://www.wikimedia.de/) employees for the [Wikidata project](https://www.wikidata.org/).

Release notes
-------------

[](#release-notes)

### 0.13.0 (2024-12-10)

[](#0130-2024-12-10)

- Drop support for PHP 7.2, 7.3
- Upgrade codesniffer rules to current `mediawiki/mediawiki-codesniffer` version (45.0.0)
- Make nullable type parameter declarations explicit for compatibility with PHP 8.4

### 0.12.3 (2022-10-24)

[](#0123-2022-10-24)

- Allow use with data-values/common 1.1.0 and data-values/interfaces 1.x

### 0.12.2 (2022-10-21)

[](#0122-2022-10-21)

- Fix `QuantityValue` and `UnboundedQuantityValue` hashes to be identical to version 0.11.1.

### 0.12.1 (2022-10-21)

[](#0121-2022-10-21)

- Allow use with data-values/data-values 3.1.0

### 0.12.0 (2022-10-21)

[](#0120-2022-10-21)

- Improve compatibility with PHP 8.1; in particular, the new `__serialize`/`__unserialize` methods are implemented now (in addition to the still supported `Serializable` interface). Make sure to also use `data-values/data-values` version 3.1.0 (or later) to keep hashes stable.
- Remove the `DATAVALUES_NUMBER_VERSION` constant.

### 0.11.1 (2021-03-31)

[](#0111-2021-03-31)

- Fix `DecimalMath::productWithoutBC` for products larger than 2^63-1 (the maximum value of a signed 64 bit integer).

### 0.11.0 (2021-03-15)

[](#0110-2021-03-15)

- Drop support for php versions older than 7.2 and HHVM

### 0.10.2 (2021-03-15)

[](#0102-2021-03-15)

- Allow use with data-values/common 1.0.0
- Allow use with data-values/interfaces 1.0.0
- Allow use with data-values/data-values 3.0.0

### 0.10.1 (2018-10-31)

[](#0101-2018-10-31)

- Allow installation together with DataValues 2.x
- DecimalMath products now get rounded to 127 characters to avoid a fatal error

### 0.10.0 (2018-04-11)

[](#0100-2018-04-11)

- Changed the float to string conversion algorithm for `DecimalValue`, `QuantityValue`, and `UnboundedQuantityValue`. Instead of a hundred mostly irrelevant decimal places it now uses PHP's "serialize\_precision" default of 17 significant digits.
- Drop compatibility with data-values/interfaces 0.1 and data-values/common 0.2

### 0.9.1 (2017-08-09)

[](#091-2017-08-09)

- Allow use with data-values/common 0.4

### 0.9.0 (2017-08-09)

[](#090-2017-08-09)

- Remove MediaWiki integration
- Strip all whitespace in DecimalParser
- Use Wikibase's CodeSniffer instead of MediaWiki's

### 0.8.3 (2017-06-26)

[](#083-2017-06-26)

- Fixed `UnboundedQuantityValue::newFromArray` not accepting mixed values.
- Deprecated `DecimalValue::newFromArray` and `UnboundedQuantityValue::newFromArray`.
- Updated minimal required PHP version from 5.3 to 5.5.9.

### 0.8.2 (2016-11-17)

[](#082-2016-11-17)

- Fixed `QuantityFormatter` suppressing ±0 for `QuantityValue`s.
- Fixed HTML escaping in `QuantityHtmlFormatter`.

### 0.8.1 (2016-08-02)

[](#081-2016-08-02)

- `UnboundedQuantityValue::newFromArray` and `QuantityValue::newFromArray` both accept serializations without and with an uncertainty interval.

### 0.8.0 (2016-08-01)

[](#080-2016-08-01)

- Added `DecimalValue::getTrimmed`.
- Added `UnboundedQuantityValue`.
    - `QuantityValue` extends `UnboundedQuantityValue`.
    - `QuantityParser` returns `UnboundedQuantityValue`s instead of always guessing an uncertainty interval.
    - `QuantityFormatter` also accepts `UnboundedQuantityValue`s.
- `QuantityParser` defaults to ±0.5 instead of ±1 when asked to guess an uncertainty interval, e.g. `1~` becomes `1±0.5`.
- `QuantityFormatter` does not round any more when the value is rendered with a known uncertainty interval.
- Fixed rounding algorithm in `DecimalMath` (rounded 1.45 to 2 instead of 1).
- `DecimalValue` constructor optionally accepts strings with no leading plus sign.
- Removed `QuantityValue::getSignificantFigures`.
- Removed `QuantityValue::newFromDecimal` (deprecated since 0.1).
- The `$vocabularyUriFormatter` parameter in the `QuantityFormatter` constructor is not nullable any more.

### 0.7.0 (2016-04-25)

[](#070-2016-04-25)

#### Breaking changes

[](#breaking-changes)

- Removed deprecated `QuantityUnitFormatter` interface.
- Removed deprecated `BasicQuantityUnitFormatter`.

#### Other changes

[](#other-changes)

- Fixed `DecimalValue` and `QuantityValue` allowing values with a newline at the end.
- `DecimalValue` strings are trimmed now, allowing any number of leading and trailing whitespace.
- Added explicit compatibility with data-values/common 0.2 and 0.3.

### 0.6.0 (2015-09-09)

[](#060-2015-09-09)

#### Breaking changes

[](#breaking-changes-1)

- `QuantityFormatter` constructor parameters changed in an incompatible way.
- `BasicNumberUnlocalizer::getUnitRegex` returns an empty string. A `QuantityParser` using this does not accept units as part of the input any more.

#### Additions

[](#additions)

- Added `QuantityHtmlFormatter`.
- `QuantityFormatter` supports an optional format string to concatenate number and unit.

#### Other changes

[](#other-changes-1)

- Deprecated `QuantityUnitFormatter` interface.
- Deprecated `BasicQuantityUnitFormatter`.
- `QuantityParser` now always trims the unit it gets via option.
- The component can now be installed together with DataValues Interfaces 0.2.x.

### 0.5.0 (2015-06-11)

[](#050-2015-06-11)

#### Breaking changes

[](#breaking-changes-2)

- `QuantityFormatter` constructor parameters changed in an incompatible way

#### Additions

[](#additions-1)

- Added `QuantityUnitFormatter` interface
- Added `BasicQuantityUnitFormatter`
- Added `QuantityFormatter::OPT_APPLY_UNIT` option
- Added `QuantityParser::OPT_UNIT` option
- Added `DecimalParser::applyDecimalExponent`
- Added `DecimalParser::splitDecimalExponent`

#### Other changes

[](#other-changes-2)

- `QuantityParser` now correctly detects precision for scientific notation
- Made constructor parameters optional in `DecimalFormatter` and `QuantityFormatter`
- Updated DataValues Interfaces dependency to 0.1.5

### 0.4.1 (2014-10-09)

[](#041-2014-10-09)

- The component can now be installed together with DataValues 1.x

### 0.4 (2014-04-24)

[](#04-2014-04-24)

- Unlocalizer interface renamed to NumberUnlocalizer
- Localizer interface renamed to NumberLocalizer
- BasicUnlocalizer interface renamed to BasicNumberUnlocalizer
- BasicLocalizer interface renamed to BasicNumberLocalizer
- Introduced FORMAT\_NAME class constants on ValueParsers in order to use them as expectedFormat
- Changed ValueParsers to pass rawValue and expectedFormat when constructing a ParseException

### 0.3 (2014-03-12)

[](#03-2014-03-12)

- Unlocalizer: added getNumberRegex() and getUnitRegex()
- Unlocalizer: replaced unlocalize() with unlocalizeNumber()
- Localizer: replaced localize() with localizeNumber()
- Localizer and Unlocalizer: no longer require the target language and options in method calls
- QuantityParser: fixed parsing of internationalized quantity strings

### 0.2 (2013-12-16)

[](#02-2013-12-16)

#### Removals

[](#removals)

- IntParser got moved to data-values/common
- FloatParser got moved to data-values/common

#### Additions

[](#additions-2)

- DecimalMath::min
- DecimalMath::max
- DecimalMath::shift
- Added option to force displaying the sign in DecimalFormatter

#### Improvements

[](#improvements)

- QuantityParser and DecimalParser now support scientific notation
- DecimalParser now supports localized parsing of values
- DecimalFormatter now supports localization of values

#### Bug fixes

[](#bug-fixes)

- Floating point errors that occurred when manipulating decimal values have been fixed. ([bug 56682](https://bugzilla.wikimedia.org/show_bug.cgi?id=56682))

### 0.1 (2013-11-17)

[](#01-2013-11-17)

Initial release with these features:

- DecimalMath
- DecimalValue
- QuantityValue
- DecimalFormatter
- QuantityFormatter
- DecimalParser
- FloatParser
- IntParser
- QuantityParser

Links
-----

[](#links)

- [DataValues Number on Packagist](https://packagist.org/packages/data-values/number)
- [DataValues Number on TravisCI](https://travis-ci.org/wmde/Number)

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity43

Moderate usage in the ecosystem

Community40

Growing community involvement

Maturity64

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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

Recently: every ~195 days

Total

24

Last Release

524d ago

PHP version history (4 changes)0.1PHP &gt;=5.3.0

0.8.3PHP &gt;=5.5.9

0.10.2PHP &gt;=7.2

0.13.0PHP &gt;=7.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/451bd4039d530fed8f9c3da91bfa519233a397d2182cdfdcad700f6cfea19b7f?d=identicon)[Jeroen De Dauw](/maintainers/Jeroen%20De%20Dauw)

![](https://www.gravatar.com/avatar/054adb441e7ee248ec924bc45fa793835c284710eb31627587fa5de21bab9e96?d=identicon)[wmde](/maintainers/wmde)

![](https://www.gravatar.com/avatar/5406ed1d40d50ffc61d67e9f5149914dbfe0b8a52bdf297299f5ccfab0a73d91?d=identicon)[thiemowmde](/maintainers/thiemowmde)

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

![](https://www.gravatar.com/avatar/8cc7ef1a982b9520bbe19e7699556cda12f528e2fcece72d0d18e79b6457794c?d=identicon)[mariushoch](/maintainers/mariushoch)

![](https://avatars.githubusercontent.com/u/22235?v=4)[Stefan Wasilewski](/maintainers/SMW)[@smw](https://github.com/smw)

![](https://www.gravatar.com/avatar/0c1ddc1309ce2f268d42afc35c3065421f8a6d21e4ca25eb338db3a297cdef1b?d=identicon)[DataValues](/maintainers/DataValues)

---

Top Contributors

[![thiemowmde](https://avatars.githubusercontent.com/u/6576639?v=4)](https://github.com/thiemowmde "thiemowmde (105 commits)")[![JeroenDeDauw](https://avatars.githubusercontent.com/u/146040?v=4)](https://github.com/JeroenDeDauw "JeroenDeDauw (63 commits)")[![mariushoch](https://avatars.githubusercontent.com/u/2446964?v=4)](https://github.com/mariushoch "mariushoch (23 commits)")[![lucaswerkmeister](https://avatars.githubusercontent.com/u/2346599?v=4)](https://github.com/lucaswerkmeister "lucaswerkmeister (20 commits)")[![manicki](https://avatars.githubusercontent.com/u/3524114?v=4)](https://github.com/manicki "manicki (17 commits)")[![addshore](https://avatars.githubusercontent.com/u/3308769?v=4)](https://github.com/addshore "addshore (15 commits)")[![chukarave](https://avatars.githubusercontent.com/u/7420952?v=4)](https://github.com/chukarave "chukarave (10 commits)")[![JonasKress](https://avatars.githubusercontent.com/u/13198391?v=4)](https://github.com/JonasKress "JonasKress (7 commits)")[![adrianheine](https://avatars.githubusercontent.com/u/139208?v=4)](https://github.com/adrianheine "adrianheine (5 commits)")[![filbertkm](https://avatars.githubusercontent.com/u/135401?v=4)](https://github.com/filbertkm "filbertkm (4 commits)")[![tobijat](https://avatars.githubusercontent.com/u/2997252?v=4)](https://github.com/tobijat "tobijat (4 commits)")[![codders](https://avatars.githubusercontent.com/u/17782?v=4)](https://github.com/codders "codders (3 commits)")[![Ladsgroup](https://avatars.githubusercontent.com/u/5351225?v=4)](https://github.com/Ladsgroup "Ladsgroup (2 commits)")[![gmelikov](https://avatars.githubusercontent.com/u/3868786?v=4)](https://github.com/gmelikov "gmelikov (2 commits)")[![JanZerebecki](https://avatars.githubusercontent.com/u/7452727?v=4)](https://github.com/JanZerebecki "JanZerebecki (2 commits)")[![micgro42](https://avatars.githubusercontent.com/u/7372507?v=4)](https://github.com/micgro42 "micgro42 (1 commits)")[![rosalieper](https://avatars.githubusercontent.com/u/15235452?v=4)](https://github.com/rosalieper "rosalieper (1 commits)")[![smalyshev](https://avatars.githubusercontent.com/u/155000?v=4)](https://github.com/smalyshev "smalyshev (1 commits)")[![legoktm](https://avatars.githubusercontent.com/u/81392?v=4)](https://github.com/legoktm "legoktm (1 commits)")[![itamargiv](https://avatars.githubusercontent.com/u/6132917?v=4)](https://github.com/itamargiv "itamargiv (1 commits)")

---

Tags

numberwikidatadatavaluesvalueparsersvalueformatters

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/data-values-number/health.svg)

```
[![Health](https://phpackages.com/badges/data-values-number/health.svg)](https://phpackages.com/packages/data-values-number)
```

###  Alternatives

[data-values/time

Time value objects, parsers and formatters

10318.6k14](/packages/data-values-time)[data-values/common

Contains common implementations of the interfaces defined by DataValuesInterfaces

11934.5k13](/packages/data-values-common)[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)[data-values/geo

Geographical value objects, parsers and formatters

20631.0k18](/packages/data-values-geo)[data-values/data-values

Defines the DataValue interface and some trivial implementations

171.0M22](/packages/data-values-data-values)[php-decimal/php-decimal

Correctly-rounded arbitrary precision decimal floating point

781.0M9](/packages/php-decimal-php-decimal)

PHPackages © 2026

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