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 returnedundefined
. – Pointy Commented Jan 19, 2015 at 5:27
3 Answers
Reset to default 6How 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 returnedundefined
. – Pointy Commented Jan 19, 2015 at 5:27
3 Answers
Reset to default 6How 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
版权声明:本文标题:jquery - How to convert string from textbox into a number using javascript? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745581184a2157320.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论