0

Possible Duplicate:
How can I format numbers as money in JavaScript?

The input string is any number which may or may not have decimal places, and we have to

place commas in appropriate places in the currency number. For example-

input - 1000        output - 1,000

input - 1000.00     output - 1,000.00

input - 1200000     output - 1,200,000

(the comma(,) separator appears after every 3 digits, ignoring decimal places).

Can we do this total work only through regular expression in javascript?

Kindly guide.

Community
  • 1
  • 1
Ranjan Sarma
  • 1,565
  • 5
  • 22
  • 36
  • 1
    THIS QUESTION IS NOT DUPLICATE. 149055 REFERS TO DOING THAT WITH OR WITHOUT REGEX AND ITS MONEY FORMATTING. – Ranjan Sarma Sep 18 '12 at 06:51
  • 3
    There is no need to be upset. That question and this question differ only by one step. You just need to use `str.replace(/(\d+){4,}/g, replacer_function)` and link the code from that answer. – Blender Sep 18 '12 at 06:53
  • Why do you insist on a regex solution? – jfriend00 Sep 18 '12 at 06:54
  • Regex is not the right way to do this. – Korvin Szanto Sep 18 '12 at 07:02
  • _"Can we do this total work only through regular expression in javascript?"_ No. But you can use `.replace()` in combination with a regular expression. If you don't like the other question that Korvin linked to, how about http://stackoverflow.com/questions/6392102/add-commas-to-javascript-output?lq=1 (some of its answers use regex). And you should look at the list of questions under the "Related" heading on the bottom right of this page. – nnnnnn Sep 18 '12 at 07:20

0 Answers0