试图了解如何使用 parseFloat 将字符串转换为数字
Trying to understand how to use parseFloat to convert string to number
我正在尝试了解如何使用 parseFloat 将字符串转换为数字——顺便说一句,它会根据用户奖励俱乐部积分而变化。
这是我目前编写的代码:
var ptsBalance = jQuery('.value.large.withCommas').text(); // get current RC points balance
var strParse = parseInt(ptsBalance, 10); // * Output should be current RC balance
alert(strParse); // alert Rewards Club Balance
var bonusPoints = 70000;
var totalPts = jQuery(strParse + bonusPoints); // sum total of Bonus and Current points
jQuery(strParse).appendTo('.currentPts');
jQuery(totalPts).appendTo('.totalPts'); // insert RC points balance after 'Current Points' text
很明显,我没有使用 pareFloat,而是使用 strParse,它对我的字符串进行四舍五入。也就是说,如何将字符串转换为数字,例如“10”、“100”、“1,000”、“10,000”等?
这是我的 Fiddle 的 link:http://jsfiddle.net/bkmills1/60vdfykq/
仍在学习中...
提前致谢!
在您的 fiddle 中,将第 4 行更改为:
var strParse = parseInt(ptsBalance, 10);
至
var strParse = parseInt(ptsBalance.replace(',',''), 10);
您可能还想删除任何其他需要的符号,甚至使用正则表达式来做到这一点。
我正在尝试了解如何使用 parseFloat 将字符串转换为数字——顺便说一句,它会根据用户奖励俱乐部积分而变化。
这是我目前编写的代码:
var ptsBalance = jQuery('.value.large.withCommas').text(); // get current RC points balance
var strParse = parseInt(ptsBalance, 10); // * Output should be current RC balance
alert(strParse); // alert Rewards Club Balance
var bonusPoints = 70000;
var totalPts = jQuery(strParse + bonusPoints); // sum total of Bonus and Current points
jQuery(strParse).appendTo('.currentPts');
jQuery(totalPts).appendTo('.totalPts'); // insert RC points balance after 'Current Points' text
很明显,我没有使用 pareFloat,而是使用 strParse,它对我的字符串进行四舍五入。也就是说,如何将字符串转换为数字,例如“10”、“100”、“1,000”、“10,000”等?
这是我的 Fiddle 的 link:http://jsfiddle.net/bkmills1/60vdfykq/
仍在学习中...
提前致谢!
在您的 fiddle 中,将第 4 行更改为:
var strParse = parseInt(ptsBalance, 10);
至
var strParse = parseInt(ptsBalance.replace(',',''), 10);
您可能还想删除任何其他需要的符号,甚至使用正则表达式来做到这一点。