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.
On Packagist:
The recommended way to use this library is via 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.*"
}
}
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.
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.
DataValues Number was created by Wikimedia Deutschland employees for the Wikidata project.
- 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
- Allow use with data-values/common 1.1.0 and data-values/interfaces 1.x
- Fix
QuantityValue
andUnboundedQuantityValue
hashes to be identical to version 0.11.1.
- Allow use with data-values/data-values 3.1.0
- Improve compatibility with PHP 8.1;
in particular, the new
__serialize
/__unserialize
methods are implemented now (in addition to the still supportedSerializable
interface). Make sure to also usedata-values/data-values
version 3.1.0 (or later) to keep hashes stable. - Remove the
DATAVALUES_NUMBER_VERSION
constant.
- Fix
DecimalMath::productWithoutBC
for products larger than 2^63-1 (the maximum value of a signed 64 bit integer).
- Drop support for php versions older than 7.2 and HHVM
- 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
- Allow installation together with DataValues 2.x
- DecimalMath products now get rounded to 127 characters to avoid a fatal error
- Changed the float to string conversion algorithm for
DecimalValue
,QuantityValue
, andUnboundedQuantityValue
. 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
- Allow use with data-values/common 0.4
- Remove MediaWiki integration
- Strip all whitespace in DecimalParser
- Use Wikibase's CodeSniffer instead of MediaWiki's
- Fixed
UnboundedQuantityValue::newFromArray
not accepting mixed values. - Deprecated
DecimalValue::newFromArray
andUnboundedQuantityValue::newFromArray
. - Updated minimal required PHP version from 5.3 to 5.5.9.
- Fixed
QuantityFormatter
suppressing ±0 forQuantityValue
s. - Fixed HTML escaping in
QuantityHtmlFormatter
.
UnboundedQuantityValue::newFromArray
andQuantityValue::newFromArray
both accept serializations without and with an uncertainty interval.
- Added
DecimalValue::getTrimmed
. - Added
UnboundedQuantityValue
.QuantityValue
extendsUnboundedQuantityValue
.QuantityParser
returnsUnboundedQuantityValue
s instead of always guessing an uncertainty interval.QuantityFormatter
also acceptsUnboundedQuantityValue
s.
QuantityParser
defaults to ±0.5 instead of ±1 when asked to guess an uncertainty interval, e.g.1~
becomes1±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 theQuantityFormatter
constructor is not nullable any more.
- Removed deprecated
QuantityUnitFormatter
interface. - Removed deprecated
BasicQuantityUnitFormatter
.
- Fixed
DecimalValue
andQuantityValue
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.
QuantityFormatter
constructor parameters changed in an incompatible way.BasicNumberUnlocalizer::getUnitRegex
returns an empty string. AQuantityParser
using this does not accept units as part of the input any more.
- Added
QuantityHtmlFormatter
. QuantityFormatter
supports an optional format string to concatenate number and unit.
- 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.
QuantityFormatter
constructor parameters changed in an incompatible way
- Added
QuantityUnitFormatter
interface - Added
BasicQuantityUnitFormatter
- Added
QuantityFormatter::OPT_APPLY_UNIT
option - Added
QuantityParser::OPT_UNIT
option - Added
DecimalParser::applyDecimalExponent
- Added
DecimalParser::splitDecimalExponent
QuantityParser
now correctly detects precision for scientific notation- Made constructor parameters optional in
DecimalFormatter
andQuantityFormatter
- Updated DataValues Interfaces dependency to 0.1.5
- The component can now be installed together with DataValues 1.x
- 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
- 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
- IntParser got moved to data-values/common
- FloatParser got moved to data-values/common
- DecimalMath::min
- DecimalMath::max
- DecimalMath::shift
- Added option to force displaying the sign in DecimalFormatter
- QuantityParser and DecimalParser now support scientific notation
- DecimalParser now supports localized parsing of values
- DecimalFormatter now supports localization of values
- Floating point errors that occurred when manipulating decimal values have been fixed. (bug 56682)
Initial release with these features:
- DecimalMath
- DecimalValue
- QuantityValue
- DecimalFormatter
- QuantityFormatter
- DecimalParser
- FloatParser
- IntParser
- QuantityParser