My function just want to format a number or string from 'xxxxx.xx' to 'xx,xxx.xx' or xxxxx.xx to 'xx,xxx.xx'
function format(text: number | string): string {
return `${+text.toLocaleString()}`;
}
console.log(format(123456)); #123,456
console.log(format(123456.78)); #123,456.78
console.log(format('123456')); #123,456
console.log(format('123456.78')); #123,456.78
=>
NaN
NaN
123456
123456.78
This works:
Number
converts a string to a number, see Number at MDN.