jComponent / Helpers / Number operations
Updated: 07. November 2018
Author: Peter Širka

Number operations

Professional Support Chat with contributors

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


Method: Number.add()

Method can perform mathematical operation.

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()

Method performs async operations from the number.

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.floor()

Method can trim decimals to a specific count.

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.

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:

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.

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

Method: Number.padRight()

Method creates converts number to String and performs padding.

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

Method: Number.parseDate()

Method creates Date object from the number.

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

Method: Number.pluralize()

Method makes pluralization.

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