[New] Added new math functions: round10() floor10() and ceil10()

This commit is contained in:
Robert von Burg 2022-06-13 16:21:34 +02:00
parent 88423eea1a
commit 7027da5b14
Signed by: eitch
GPG Key ID: 75DB9C85C74331F7
2 changed files with 41 additions and 1 deletions

View File

@ -1,7 +1,7 @@
{
"name": "strolch-wc-util-behavior",
"description": "Strolch Polymer Util Behaviors",
"version": "0.3.3",
"version": "0.3.4",
"authors": [
"Robert von Burg"
],

View File

@ -195,6 +195,46 @@
return Math.round(value * 1000) / 1000;
},
/**
* Decimal adjustment of a number.
*
* @param {String} type The type of adjustment.
* @param {Number} value The number.
* @param {Integer} exp The exponent (the 10 logarithm of the adjustment base).
* @returns {Number} The adjusted value.
*/
_decimalAdjust: function (type, value, exp) {
// If the exp is undefined or zero...
if (typeof exp === 'undefined' || +exp === 0) {
return Math[type](value);
}
value = +value;
exp = +exp;
// If the value is not a number or the exp is not an integer...
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
return NaN;
}
// Shift
value = value.toString().split('e');
value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
// Shift back
value = value.toString().split('e');
return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
},
// Decimal round
round10: function (value, exp) {
return this._decimalAdjust('round', value, exp);
},
// Decimal floor
floor10: function (value, exp) {
return this._decimalAdjust('floor', value, exp);
},
// Decimal ceil
ceil10: function (value, exp) {
return this._decimalAdjust('ceil', value, exp);
},
//
// i18n functions
//