0

I have a textbox with input value of 20,466,000.00 . I want to return only the value of 20466000 without a comma and a decimal point. I tried the following :

// Get value
var nca_balance = $("#nca-balance").text();
var nca_amount = parseInt(nca_balance.replace(",", ""),10);

alert(nca_amount);

But it only returns the value of 20466 ? Why ? My expected result must be 20466000. Please help with my code. Thanks

1
  • You retrieve the value of an <input> with .val(), not .text(); if that really were what your code looked like the .text() call would have returned undefined. Commented Jan 19, 2015 at 5:27

3 Answers 3

6

How about this one:

parseInt("20,466,000.00".replace(/,/g, ""), 10)
Sign up to request clarification or add additional context in comments.

Comments

4

Because replace is replacing the first comma by default use g as global option in replace regular expression /,/g like,

var nca_balance = $("#nca-balance").text();
var nca_amount = parseInt(nca_balance.replace(/,/g, ""),10);
alert(nca_amount);

    var nca_balance = $("#nca-balance").text();
    var nca_amount = parseInt(nca_balance.replace(/,/g, ""), 10);
    alert(nca_amount);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="nca-balance">20,466,000.00<span>

Comments

0

You have to use regular expression instead of replace function because replace function will replace only first matching character.

nca_balance = "20,466,000.00";
var nca_amount = parseInt(nca_balance.replace(/,/g, ''));
alert(nca_amount);

Regular expression /,/g will replace all comma with your provided character globally.

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.