0

I am newbie to javascript and struggling to write regex pattern for the requirement

2,4 = 24.00

2.4 = 2.40

2.4.5 = 2.40

2.5,5 = 2.50

2,5.7 = 25.70

45.56.34 = 45.56

13.,0 = 13.00

13,.0 = 13.00

Only digits and comma and dot are allowed. it doesn't matter how they enter But i have to convert this into proper currency format xxx,xxx,xxx,xx.xx

edited: to clarify doubts

3
  • 1
    2,4 = 24.00, but 2,5.7 = 2.50. I don't understand something. Commented Jul 28, 2011 at 13:34
  • 1
    You have not given enough information. For example, why does 2,5.7 become 2.50 instead of 25.70, and why does 2.4.5 become 2.40 instead of 2.45 Commented Jul 28, 2011 at 13:35
  • 1
    Seems like there are a few conflicting requirements here. Commented Jul 28, 2011 at 13:36

2 Answers 2

3

Try with:

var input  = '2,4';
var output = parseFloat( input.replace(",", ".").replace("..", ".") ).toFixed(2);
Sign up to request clarification or add additional context in comments.

2 Comments

A few of these examples will take more work than that, eg 13.,0 and 45.56.34
Fair do's - wasn't expecting parseFloat to handle 45.56.34 mostly. +1
0

have a look at the below web site you will find the Regex you are looking for

http://www.regexlib.com/DisplayPatterns.aspx?cattabindex=2&categoryId=3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.