【问题标题】:Trying to understand how to use parseFloat to convert string to number试图了解如何使用 parseFloat 将字符串转换为数字
【发布时间】:2015-04-01 19:54:17
【问题描述】:

我正在尝试了解如何使用 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 的链接:http://jsfiddle.net/bkmills1/60vdfykq/

还在学习中……

提前致谢!

【问题讨论】:

  • 如果您要进行大量货币/数字解析,使用库可能会有所帮助。像 one 这样的东西可能会有所帮助。

标签: javascript jquery string numbers


【解决方案1】:

在你的小提琴中,将第 4 行从:

var strParse = parseInt(ptsBalance, 10);

var strParse = parseInt(ptsBalance.replace(',',''), 10);

您可能还想删除任何其他需要的符号,或者甚至使用正则表达式来做到这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多