Cater for negative values in number formats (#415)
* Cater for negative values in number formats * Handle edge-case where -text is provided
This commit is contained in:
@@ -5,9 +5,14 @@
|
||||
const NUMBER_REGEX = new RegExp('(\\d+?)(?=(\\d{3})+(?!\\d)|$)', 'g');
|
||||
|
||||
export default function formatDecimal (value: string): string {
|
||||
const matched = value.match(NUMBER_REGEX);
|
||||
// We can do this by adjusting the regx, however for the sake of clarity
|
||||
// we rather strip and re-add the negative sign in the output
|
||||
const isNegative = value[0] === '-';
|
||||
const matched = isNegative
|
||||
? value.substr(1).match(NUMBER_REGEX)
|
||||
: value.match(NUMBER_REGEX);
|
||||
|
||||
return matched
|
||||
? matched.join(',')
|
||||
? `${isNegative ? '-' : ''}${matched.join(',')}`
|
||||
: value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user