0

parseInt convertion 1,000.00 gives 1 but i want 1000 , what is the problem?

<html>
    <body>
      <script type="text/javascript">
        alert(parseInt("1,000.00")) ;  // always gives output 1
      </script>
    </body>
</html>  

thank you

Shaik Matheen
  • 1,233
  • 15
  • 14

2 Answers2

5

How about this?

parseInt("1,000.00".replace(/\,/g,''), 10)

If you want to preserve the decimal then,

parseFloat("1,000.00".replace(/\,/g,''), 10)
VJAI
  • 32,167
  • 23
  • 102
  • 164
  • 1
    A person with your rep should know that this a very basic question and **should be** marked as duplicate instead of answering – Rajesh Feb 02 '17 at 08:00
  • 1
    Thank you sir. Next time be polite on your comment and it's a request. – VJAI Feb 02 '17 at 08:01
  • I'm sorry for being rude but you have been here more than me. You should lead by example. Answering a duplicate is not a good practice. – Rajesh Feb 02 '17 at 08:03
  • @VJAI ok, this works. But what if I'm trying to do this in a loop? (not knowing which number is the one with ",") – Robert Feduș Mar 28 '20 at 18:11
0

Remove the comma first and execute the parseInt. Try this:

parseInt("1,000.00".replace(",",""));
prtdomingo
  • 971
  • 4
  • 14
  • 2
    what about `parseInt("1000,00")`? – Gene R Feb 02 '17 at 07:54
  • 3
    Your replace will only change one ",". You should use `parseInt("1,000.00".replace(/,/g, "")) ;` – Shubham Feb 02 '17 at 07:55
  • what it means replace(/,/g, "") ? – Shaik Matheen Feb 02 '17 at 07:56
  • 1
    @ShaikMatheen `//` is used to define a `regex`. `g` is a flag that will check string recursively. so `(/,/g, "")` will recursively look for `,` and replace it with `""` – Rajesh Feb 02 '17 at 07:57
  • Ah yes. I was focused on the given number on the post. But the best thing to do would be the regex which was commented by @Shubham – prtdomingo Feb 02 '17 at 07:58
  • Also @prtdomingo, please do not answer question that are very basic. There is a high possibility that they have already been answered. You should search such post and share them as comment until you have permission to close a question. Post that please mark it duplicate – Rajesh Feb 02 '17 at 07:59
  • Sure. Would do it next time. Thanks! – prtdomingo Feb 02 '17 at 08:00