PHPackages                             nicmart/numbers - 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. nicmart/numbers

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

nicmart/numbers
===============

Format numbers in various formats, like scientific notation or unit-suffix notation

v0.1.0(12y ago)54172.3k↓11.5%8[1 issues](https://github.com/nicmart/Numbers/issues)1MITPHPPHP &gt;=5.3.3

Since Sep 15Pushed 9y ago3 watchersCompare

[ Source](https://github.com/nicmart/Numbers)[ Packagist](https://packagist.org/packages/nicmart/numbers)[ RSS](/packages/nicmart-numbers/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (1)

Numbers
=======

[](#numbers)

[![Build Status](https://camo.githubusercontent.com/1cb9ffa61c2a1c14e2d8bc6a674410c60679eb1c8e6d7eff265628c1f35ee32b/68747470733a2f2f7472617669732d63692e6f72672f6e69636d6172742f4e756d626572732e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/nicmart/Numbers)[![Coverage Status](https://camo.githubusercontent.com/514fee17aa9fe4a30a59547048a05cf8de816a7361221d569227ffd385f0eea2/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6e69636d6172742f4e756d626572732f62616467652e706e673f6272616e63683d6d6173746572)](https://coveralls.io/r/nicmart/Numbers?branch=master)[![Scrutinizer Quality Score](https://camo.githubusercontent.com/4f693f1c07b856b81021d649972fa76c710e5434585119acdc6298125dda3aa0/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e69636d6172742f4e756d626572732f6261646765732f7175616c6974792d73636f72652e706e673f733d36306463356462333735356631643039373839666230356534346264396234313363663139313739)](https://scrutinizer-ci.com/g/nicmart/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
-----

[](#usage)

First, instantiate an object of the `Number` class. You can do it in two ways: directly or using the `n` static method:

```
use Numbers\Number;
$n = new Number(3.1415926535898);
$n = Number::n(3.1415926535898);
```

You can then retrieve the underlying float/int value using the `get` method:

```
var_dump($n->get()); //double(3.1415926535898)
```

### Significant figures

[](#significant-figures)

You can set the number of [significant figures](http://en.wikipedia.org/wiki/Significant_figures) using the method `round`.

```
$n->round(5)->get(); // returns 3.1416
```

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:

```
(new Number(0.000123456))->round(5)->get(); // returns 0.00012346
```

### Scientific notation

[](#scientific-notation)

You can easily convert a `Number` to [Scientific Notation](http://en.wikipedia.org/wiki/Scientific_notation):

```
$sciNotation = Number::n(1234.567)->getSciNotation();
echo $sciNotation->significand; // 1.234567
echo $sciNotation->magnitude; // 4
```

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

[](#suffix-notation)

With suffix notation you can convert a number to a format using the [metric prefix notation](http://en.wikipedia.org/wiki/Metric_prefix). 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](http://en.wikipedia.org/wiki/International_System_of_Units) symbols are supported.

```
// Prints "1.23k"
echo Number::n(1234.567)->round(3)->getSuffixNotation();

// Prints "79G"
echo Number::n(79123232123)->round(2)->getSuffixNotation();

// Prints "123.4µ"
echo Number::n(0.0001234)->getSuffixNotation();
```

### Format with thousands and decimals separator

[](#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.

```
// Prints "123,123.23"
echo Number::n(123123.23)->format();

// Prints "123 123,23"
echo Number::n(123123.23)->format(',', ' ');
```

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

[](#other-functions)

#### Floor and Ceil

[](#floor-and-ceil)

They behave like their [mathematical counterparts](http://en.wikipedia.org/wiki/Floor_and_ceiling_functions)and the builtin php functions:

```
// Returns "123123"
Number::n(123123.23)->floor()->get();

// Returns "123124"
Number::n(123123.23)->ceil()->get();
```

#### Magnitude

[](#magnitude)

Gives the order of magnitude of the number (that is equal to the exponent of 10 in the Scientific Notation):

```
// Returns 6
Number::n(123123.23)->getMagnitude();

// Returns -2
Number::n(0.01232)->getMagnitude();
```

#### n-th digit

[](#n-th-digit)

Gives the n-th digit of the number in a given base

```
// Returns 4
Number::n(1234.5678)->getDigit(0);
// Returns 3
Number::n(1234.5678)->getDigit(1);
//Returns 7
Number::n(1234.5678)->getDigit(-3);

// Second optional arg is the base (default is 10)
// Returns 1
Number::n(bindec('10110101'))->getDigit(0, 2);
// Returns 0
Number::n(bindec('10110101'))->getDigit(6, 2);
```

#### Sign

[](#sign)

The [sign function](https://en.wikipedia.org/wiki/Sign_function), as defined in mathematics

```
// Returns 1
Number::n(12312)->getSign();

// Returns -1
Number::n(-0.0023)->getSign();

// Returns 0
Number::n(0)->getSign();
```

#### Apply

[](#apply)

Apply a callback to the underlying scalar number, in a [Monad](https://en.wikipedia.org/wiki/Monad) fashion:

```
$double = function($n){ return 2 * $n; };
// Returns 16
Number::n(4)->apply($double)->apply($double)->get();
```

Install
-------

[](#install)

The best way to install Numbers is [through composer](http://getcomposer.org).

Just create a composer.json file for your project:

```
{
    "require": {
        "nicmart/numbers": "dev-master"
    }
}
```

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](http://getcomposer.org/doc/00-intro.md#globally).

Then you can include the autoloader, and you will have access to the library classes:

```
