koesie10/unit-converter

v0.1 2016-11-15 19:57 UTC

This package is auto-updated.

Last update: 2024-05-05 18:27:47 UTC


README

Build Version License Code Coverage Code Quality Downloads

This is a SI unit converter for PHP, that will convert all kinds of units from one into the other, with support for composite units.

Installation

Install with Composer:

composer require koesie10/unit-converter

Usage

Simply create a \Vlaswinkel\UnitConverter\UnitConverter object and call the convert method:

$converter = new \Vlaswinkel\UnitConverter\UnitConverter();

echo $converter->convert('1', 'm/s', 'km/h'); // prints 3.6, as expected

This library will try to use BC math if it is available, which is highly recommended for more accurate results. In that case, it is recommended to always supply strings to the convert function, otherwise there will still be floating point errors.

Note: If you do not get proper results, check that you have set the scale for BC math correctly. It is recommended to set the value to at least 3, but for more precision, it is recommended to increase the value more.

If you would like to use the default PHP math functions, which will have floating point errors but will not require a scale, you can instantiate the UnitConverter as follows:

$converter = new \Vlaswinkel\UnitConverter\UnitConverter(null, new \Vlaswinkel\UnitConverter\Math\DefaultMathWrapper());

Running tests

You can run automated unit tests using PHPUnit after installing dependencies:

vendor/bin/phpunit

License

This library is licensed under the MIT license. See the LICENSE file for details.