UNPKG

621 BJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3// eslint-disable-next-line prefer-regex-literals
4const NUMBER_REGEX = new RegExp('(\\d+?)(?=(\\d{3})+(?!\\d)|$)', 'g');
5export function formatDecimal(value) {
6 // We can do this by adjusting the regx, however for the sake of clarity
7 // we rather strip and re-add the negative sign in the output
8 const isNegative = value[0].startsWith('-');
9 const matched = isNegative ? value.substring(1).match(NUMBER_REGEX) : value.match(NUMBER_REGEX);
10 return matched ? `${isNegative ? '-' : ''}${matched.join(',')}` : value;
11}
\No newline at end of file