I'm trying to write a simple function that converts currency values to a code.
I've managed to do the conversion part with .replace(). See jsfiddle https://jsfiddle.net/j9cdrq73/3/
What I want to do is, replace the 2nd repeating character(s) with letter 'Z'
Edit
After the dot (. decimal point) it should reset. 50000 DIZZZ.IZ NOTE the last IZ* see following examples
number => after convert =>final (what it should give)
--------- ------------- ----------------------------
So 50000 => DI,III.II => DI,ZZZ.IZ
2677.99 => B,EFF.HH => B,EFZ.HZ
366666.22 => CEE,EEE.BB => CEZ,ZZZ.BZ
Current code
function number_to_code(s) {
// converting the value to something like 50000.00 to look like currency
// next replace it with the codes as bellow.
s = parseFloat(s).toFixed(2).replace(/\d/g, m => ({
'0': 'I',
'1': 'A',
'2': 'B',
'3': 'C',
'5': 'D',
'6': 'E',
'7': 'F',
'8': 'G',
'9': 'H'
})[m]);
// grouping it with ,
s = s.replace(/\B(?=(\w{3})+(?!\w))/g, ",");
return s;
}
Better if we can do it all in a single regex to improve efficiency.