2
I need to format whole numbers, example:
let num = 1234567;
// saída 1.234.567
However, if the number you are going to have has decimal places that appear to no more than two decimal places. Example:
let num = 1234567.891
// saída 1.234.567,89
let num2 = 123.4
// saída 123,4
That is, if an integer shows formatted with no decimal places, now if a number with decimal places comes up, show 2 places at most.
My code works to some extent, when the size passes 6 it does not format anymore. It is doing so:
let num = 1234567;
// saída 1234.567
Below my code:
export function fmtoNumero(val, n = 2, x = 3, s = '.', c = ',') {
let numval = Number(val);
let re = '\\d(?=(\\d{' + x + '})(\\D|$))';
numval = numval.toString().replace('.', c);
numval = numval.replace(new RegExp(re, 'g'), '$&' + (s || ','));
return numval;
}