UNPKG

842 BJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.formatDecimal = formatDecimal;
7// Copyright 2017-2022 @polkadot/util authors & contributors
8// SPDX-License-Identifier: Apache-2.0
9// eslint-disable-next-line prefer-regex-literals
10const NUMBER_REGEX = new RegExp('(\\d+?)(?=(\\d{3})+(?!\\d)|$)', 'g');
11/**
12 * @name formatDecimal
13 * @description Formats a number into string format with thousand seperators
14 */
15
16function formatDecimal(value) {
17 // We can do this by adjusting the regx, however for the sake of clarity
18 // we rather strip and re-add the negative sign in the output
19 const isNegative = value[0].startsWith('-');
20 const matched = isNegative ? value.substring(1).match(NUMBER_REGEX) : value.match(NUMBER_REGEX);
21 return matched ? `${isNegative ? '-' : ''}${matched.join(',')}` : value;
22}
\No newline at end of file