UNPKG

@feugene/mu

Version:

Helpful TS utilities without dependencies

34 lines 1.25 kB
import isInteger from '../is/isInteger.mjs'; /** * Formatting number * @param {String|Number} value * @param {Number} decimals * @param {String} decPoint * @param {String} thousandsSeparator * @param {Boolean} clearDecimals * @returns {string} */ export default function number(value, decimals = 2, decPoint = '.', thousandsSeparator = ',', clearDecimals = false) { decimals = isNaN(decimals) ? 2 : Math.abs(decimals); const num = Number(value); const sign = num < 0 ? '-' : ''; value = Math.abs(+num || 0); const intPart = parseInt(num.toFixed(decimals), 10) + ''; // const intPartStr = intPart + '' const j = intPart.length > 3 ? intPart.length % 3 : 0; return (sign + (j ? intPart.slice(0, j) + thousandsSeparator : '') + intPart.slice(j).replace(/(\d{3})(?=\d)/g, '$1' + thousandsSeparator) + (decimals ? clearDecimals && isInteger(num) ? '' : decPoint + Math.abs(num - +intPart) .toFixed(decimals) .slice(2) : '')); } export function numberRus(value, decimals = 2) { return number(value, decimals, '.', ' ', true); } //# sourceMappingURL=number.mjs.map