# __Number__ operations

[![+Professional Support](https://www.totaljs.com/img/badge-support.svg)](https://www.totaljs.com/support/) [![+Chat with contributors](https://www.totaljs.com/img/badge-chat.svg)](https://messenger.totaljs.com)

Here is the list of all registered `Number` prototypes in jComponent library:

- [`Number.add(value, [decimals])`](#method-number-add-)
- [`Number.async(fn, [callback])`](#method-number-async-)
- [`Number.currency(currency)`](#method-number-currency-)
- [`Number.floor(count)`](#method-number-floor-)
- [`Number.format(decimals, [separatorThousands], [separatorDecimals])`](#method-number-format-)
- [`Number.padLeft(count, [char])`](#method-number-padleft-)
- [`Number.padRight(count, [char])`](#method-number-padright-)
- [`Number.parseDate([offset])`](#method-number-parsedate-)
- [`Number.pluralize(zero, one, few, other)`](#method-number-pluralize-)

---

### Method: `Number.add()`

Method can perform mathematical operation.

```javascript
Number.add(value, [decimals]);
// @value {String}
// @decimals {Boolean} optional
// return {Number}

(10).add('-10%'); // 9
(10).add('+15%'); // 15
(10).add('+5');   // 15
(10).add('-100'); // -90
(10).add('*2');   // 20
(10).add('/2');   // 5
(10).add('20%');  // 2
```

### Method: `Number.async()`

__`REMOVED in v18`__ Method performs async operations from the number.

```javascript
Number.async(fn, [callback]);
// @fn {Function(number)}
// @callback {Function} optional

(5).async(function(number, next) {
	console.log(number);
	setTimeout(next, 100);
}, function() {
	console.log('DONE');
});

// 0
// 1
// 2
// 3
// 4
```

### Method: `Number.currency()`

`+v17` Method formats a number to currency.

```javascript
Number.currency(currency);
// @currency {String}
// return {String}

// First, you need to have currency formatters declared:
DEF.currencies.eur = function(val) {
	// @val {Number}
	// return {String}
	return val.format(2) + ' &euro;';
};

DEF.currencies.usd = function(val) {
	// @val {Number}
	// return {String}
	return '$ ' + val.format(2);
};

console.log((2121).currency('usd'));
console.log((2121).currency('eur'));
```

### Method: `Number.floor()`

Method can trim decimals to a specific count.

```javascript
Number.floor(count);
// @count {Number} count of decimals
// return {Number}

(10.123).floor(0); // 10
(10.123).floor(1); // 10.1
(10.123).floor(2); // 10.12
(10.123).floor(5); // 10.123
```

### Method: `Number.format()`

Method formats number.

```javascript
Number.format(decimals, [separatorThousands], [separatorDecimals]);
// @separatorThousands {String} optional, default "MAIN.defaults.thousandsseparator" --> " "
// @separatorDecimals {String} optional, default "MAIN.defaults.decimalseparator" --> "." 
// return {String}

(100).format(2);      // 1 000.00
(10000).format(3);    // 10 000.000
(1000.123).format(1); // 1 000.1
(5000.123).format(0); // 5 000
```

jComponent `+v12.0.6` supports environment variables for the formatting, a simple example:

```javascript
ENV('myformat', { decimals: 3, separator: ',', decimalseparator: '.' });
console.log((1000).format('[myformat]'));
// Output: 1.000,000
```

### Method: `Number.padLeft()`

Method creates converts number to String and performs padding.

```javascript
Number.padLeft(count, [char]);
// @count {Number}
// @char {String} optional, default " "
// return {String}
```

### Method: `Number.padRight()`

Method creates converts number to String and performs padding.

```javascript
Number.padRight(count, [char]);
// @count {Number}
// @char {String} optional, default " "
// return {String}
```

### Method: `Number.parseDate()`

Method creates `Date` object from the number.

```javascript
Number.parseDate([offset]);
// @offset {Number} optional, default 0
// return {Date}
```

### Method: `Number.pluralize()`

Method makes pluralization.

```javascript
Number.pluralize(zero, one, few, other);
// @zero {String}
// @one {String}
// @few {String}
// @other {String}
// return {String}

// or +v16
Number.pluralize(arr);
// @arr {String Array} in the form ['zero', 'one', 'few', 'other']

(1).pluralize('# zero', '# one', '# few', '# other');   // 1 one
(3).pluralize('# zero', '# one', '# few', '# other');   // 3 few
(10).pluralize('# zero', '# one', '# few', '# other');  // 10 other

// Or with array
(10).pluralize(['# zero', '# one', '# few', '# other']);  // 10 other
```