0

I'm sorry, but I can't find working dot replacement on stackoverflow. Peoples ask about replacing

var str = '. 950.000.000, -';
str = str.replace(/\./gi, '');
alert(parseInt(str)); // yes, it works & will output correctly

But, it wouldn't works, when my 'str' var is : Rp. 950.000,- . It is currency format in my area, and i want to do math with it. I do this, and not work:

var str = 'Rp. 950.000, -';
str = str.replace(/\./gi, '');// i dont know, but the str values now is nothing
alert(parseInt(str)); // sure, it outputs nothing

I just want to replace all dots (so it will not bothers math operation, because dot is a decimal on number).

4
  • output of str.replace is "Rp 950000, -" Commented May 31, 2013 at 23:55
  • You're trying to use parseInt on something that is not a number. Your alert will print NaN. Just remove the parseInt if all you want is: Rp 950000, - Commented May 31, 2013 at 23:56
  • This approach sounds culturally-broken (or culture-centric, depending on how it's viewed). How should 1.234,56 (or 1,234.56) be treated? Commented Jun 1, 2013 at 0:01
  • Thank you Guys! all i want is 950000 so i can math it. Answers below answered the question :D thank you Commented Jun 1, 2013 at 0:34

2 Answers 2

2

Why not replace everything that is not a number with \D?

var str = 'Rp. 950.000, -';
str = str.replace(/\D/gi, '');// i dont know, but the str values now is nothing
alert(parseInt(str, 10));

Working Example http://jsfiddle.net/e8dMD/1/

Sign up to request clarification or add additional context in comments.

1 Comment

Unless you have decimal places like 950.000,25. But that's not what Jahe asked...
0

After removing all the dots in the string 'Rp. 950.000, -' you will be left with 'Rp 950000, -'. If you try to use this string for parseInt() it will fail because of the letters at the beginning and the other characters at the end. If you want to remove all non-digit characters from you string you can use the following:

str = str.replace(/\D/g, '');

After this parseInt() should work fine and give you 950000.

2 Comments

Thank you for answers. the answer of others also good. Thank you all. I choose this because this explain why my source not works. Thanks
parseInt would not fail because of non-digit characters at the end - it ignores any trailing non-digits. Leading non-digits are a different story

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.