admin管理员组

文章数量:1023758

I have a textbox with input value of 20,466,000.00 . I want to return only the value of 20466000 without a ma 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

I have a textbox with input value of 20,466,000.00 . I want to return only the value of 20466000 without a ma 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

Share Improve this question asked Jan 19, 2015 at 5:24 LarcyLarcy 2993 gold badges9 silver badges18 bronze badges 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. – Pointy Commented Jan 19, 2015 at 5:27
Add a ment  | 

3 Answers 3

Reset to default 6

How about this one:

parseInt("20,466,000.00".replace(/,/g, ""), 10)

Because replace is replacing the first ma 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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="nca-balance">20,466,000.00<span>

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 ma with your provided character globally.

I have a textbox with input value of 20,466,000.00 . I want to return only the value of 20466000 without a ma 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

I have a textbox with input value of 20,466,000.00 . I want to return only the value of 20466000 without a ma 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

Share Improve this question asked Jan 19, 2015 at 5:24 LarcyLarcy 2993 gold badges9 silver badges18 bronze badges 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. – Pointy Commented Jan 19, 2015 at 5:27
Add a ment  | 

3 Answers 3

Reset to default 6

How about this one:

parseInt("20,466,000.00".replace(/,/g, ""), 10)

Because replace is replacing the first ma 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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="nca-balance">20,466,000.00<span>

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 ma with your provided character globally.

本文标签: jqueryHow to convert string from textbox into a number using javascriptStack Overflow