@opengis/fastify-table
Version:
core-plugins
55 lines (48 loc) • 1.85 kB
JavaScript
/**
* Форматування до числа
*
* @summary Форматування до числа
* @priority 0
* @alias formatNumber
* @type helper
* @example
* {{{formatNumber space}}}
* @example
* {{{formatNumber amount}}}
* @example
* {{formatNumber size_log}}
* @example
* {{formatNumber (coalesce proj_finish 0) }}
* @param {String|Number} data Число для формування
* @param {Number} round Параметр округлення
* @param {String} style Стиль форматування (currency, decimal, percent, unit)
* @param {String} currency Валюта (наприклад, EUR)
* @param {String} unit Одиниця вимірювання (наприклад, liter)
* @param {String} locale Мова (наприклад, ua, en)
* @param {String} unitDisplay Стиль відображення одиниці (short, narrow, long)
* @param {String} notation Стиль скорочення чисел (compact)
* @returns {String} Відформатоване число
*/
export default function formatNumber(data, options) {
const TP = typeof data;
if (!data || (TP !== 'number' && TP !== 'string')) return '';
if (TP === 'string') {
data = data.replace(',', '.') - 0;
}
const round = options.hash?.round ?? 2;
const style = options.hash?.style ?? 'decimal';
const currency = options.hash?.currency ?? 'UAH';
const unit = options.hash?.unit ?? 'liter';
const locale = options.hash?.locale ?? 'ua';
const unitDisplay = options.hash?.unitDisplay ?? 'short';
const notation = options.hash?.notation ?? 'standard';
const formatter = new Intl.NumberFormat(locale, {
style: style,
currency: currency,
unit: unit,
unitDisplay: unitDisplay,
notation: notation,
maximumFractionDigits: round
});
return formatter.format(data - 0);
}