tags:

views:

204

answers:

2

Hello guys,

I need to format numbers so that thousands were not separated by anything and hundreds were separated with a dot. For example

1234.56 12.34 123

I wrote the following ReqExp

amountValue.replace(/^(\d+)[,.](\d{3})[.,](\d{2})$/,'$1' + '$2' +'.'+'$3').replace(/^(\d+),(\d{2})$/,'$1' +'.'+'$2');

If there a way to make it shorter?

Thank you!

+1  A: 

I think this might work:

amountValue.replace(/^(\d*)[,.]?(\d{0,3})[.,](\d{2})$/,'$1' + '$2' +'.'+'$3');

Try it against your data.

ck
+1  A: 

I would just remove any non numeric character that’s not the decimal point:

amountValue.replace(/[^0-9](?!\d{2}$)/, '').replace(/,(?=\d{2}$)/, '.');
Gumbo
This won't work in international (European) formats - the decimal is the thousand separator and the comma is the decimal separator.
ck
Have you tested it? It works fine for me with values like "123.456,78", "123,456.78", "123,45" or "123.45"
Gumbo