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.
QuantityValue
and UnboundedQuantityValue
hashes to be identical to version 0.11.1.__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.DATAVALUES_NUMBER_VERSION
constant.DecimalMath::productWithoutBC
for products larger than 2^63-1 (the maximum value of a signed 64 bit integer).DecimalValue
, QuantityValue
, and
UnboundedQuantityValue
. Instead of a hundred mostly irrelevant decimal places it now uses PHP's
"serialize_precision" default of 17 significant digits.UnboundedQuantityValue::newFromArray
not accepting mixed values.DecimalValue::newFromArray
and UnboundedQuantityValue::newFromArray
.QuantityFormatter
suppressing ±0 for QuantityValue
s.QuantityHtmlFormatter
.UnboundedQuantityValue::newFromArray
and QuantityValue::newFromArray
both accept
serializations without and with an uncertainty interval.DecimalValue::getTrimmed
.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.DecimalMath
(rounded 1.45 to 2 instead of 1).DecimalValue
constructor optionally accepts strings with no leading plus sign.QuantityValue::getSignificantFigures
.QuantityValue::newFromDecimal
(deprecated since 0.1).$vocabularyUriFormatter
parameter in the QuantityFormatter
constructor is not nullable any more.QuantityUnitFormatter
interface.BasicQuantityUnitFormatter
.DecimalValue
and QuantityValue
allowing values with a newline at the end.DecimalValue
strings are trimmed now, allowing any number of leading and trailing whitespace.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.QuantityHtmlFormatter
.QuantityFormatter
supports an optional format string to concatenate number and unit.QuantityUnitFormatter
interface.BasicQuantityUnitFormatter
.QuantityParser
now always trims the unit it gets via option.QuantityFormatter
constructor parameters changed in an incompatible wayQuantityUnitFormatter
interfaceBasicQuantityUnitFormatter
QuantityFormatter::OPT_APPLY_UNIT
optionQuantityParser::OPT_UNIT
optionDecimalParser::applyDecimalExponent
DecimalParser::splitDecimalExponent
QuantityParser
now correctly detects precision for scientific notationDecimalFormatter
and QuantityFormatter
Initial release with these features: