Download the PHP package nicmart/numbers without Composer
On this page you can find all versions of the php package nicmart/numbers. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nicmart/numbers
More information about nicmart/numbers
Files in nicmart/numbers
Package numbers
Short Description Format numbers in various formats, like scientific notation or unit-suffix notation
License MIT
Informations about the package numbers
Numbers
Numbers provides a simple and powerful way to convert numbers in various string formats, like scientific notation or unit-suffix notation.
It also gives you control on numbers precision (that's different of the numbers of decimals!), making it simple to format numbers as you want in your view layer.
For installing instructions, please go to the end of this README.
Usage
First, instantiate an object of the Number
class. You can do it in two ways: directly or using the n
static method:
You can then retrieve the underlying float/int value using the get
method:
Significant figures
You can set the number of significant figures using the method round
.
As you can see, the round
method works differently from the php builtin round
function, since you are not setting the number of decimals, but the number of significant figures:
Scientific notation
You can easily convert a Number
to Scientific Notation:
A SciNotation
objects convert themselves to html when casted to strings:
(string) Number::n(1234.567)->getSciNotation()
→ 1.234567 × 104
(string) Number::n(0.000023)->getSciNotation()
→ 2.3 × 10-5
Suffix notation
With suffix notation you can convert a number to a format using the metric prefix notation. What you will get is a number followed by a suffix that indicates the magnitude of that number, using the "kilo", "mega", etc... symbols. All the SI symbols are supported.
Format with thousands and decimals separator
The format
method works like number_format
, but without the hassle of specifying the
number of decimals. The number of significant figures will be used instead. Furthermore, it
will not print trailing zeros in the decimal part.
By default format
fallbacks to .
and ,
separators if some argument is missing. If you want instead
to fallback to the current locale settings of the machine, you can use localeFormat
.
Other functions
Floor and Ceil
They behave like their mathematical counterparts and the builtin php functions:
Magnitude
Gives the order of magnitude of the number (that is equal to the exponent of 10 in the Scientific Notation):
n-th digit
Gives the n-th digit of the number in a given base
Sign
The sign function, as defined in mathematics
Apply
Apply a callback to the underlying scalar number, in a Monad fashion:
Install
The best way to install Numbers is through composer.
Just create a composer.json file for your project:
Then you can run these two commands to install it:
$ curl -s http://getcomposer.org/installer | php
$ php composer.phar install
or simply run composer install
if you have have already installed the composer globally.
Then you can include the autoloader, and you will have access to the library classes: