Reputation: 299
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
Upvotes: 0
Views: 1395
Reputation: 18601
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.
Upvotes: 0
Reputation: 40639
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>
Upvotes: 4
Reputation: 2824
How about this one:
parseInt("20,466,000.00".replace(/,/g, ""), 10)
Upvotes: 6