JSFiddle - React, Tailwind, and code Playground
by Gerald Gillespie
TypeScript
interface currencyParse{
( value: string ): number;
}
const currencyParser: currencyParse = (value) =>{
const decimalCharactersTest = (v)=>/[.,]/.test(v);
const anyMantissaLengthRgx = /\D(?=\d{2,}$)/;
const [characteristic, mantissa] = decimalCharactersTest(value)? value.split(anyMantissaLengthRgx): [value];
const modifiedCharacteristic = characteristic.replace(/\D/g,'');
return Number([modifiedCharacteristic,mantissa].join('.');
}
const myNumbers = [
'1 000.12987',
'1,000.128978979',
'1.000,12987987',
'1 000,12987897',
'1 0000'
];
const translated = myNumbers.map( (n)=>{
console.log( currencyParser(n));
});