试图了解如何使用parseFloat将字符串转换为数字

Trying to understand how to use parseFloat to convert string to number

本文关键字:字符串 转换 数字 parseFloat 了解 何使用      更新时间:2023-09-26

我正在尝试了解如何使用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"、"1000"、"10000"等的数字。?

以下是我的Fiddle链接:http://jsfiddle.net/bkmills1/60vdfykq/

还在学习。。。

提前感谢!

在小提琴中,将第4行从:更改为

var strParse = parseInt(ptsBalance, 10);

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

您可能还想删除所需的任何其他符号,甚至可以使用正则表达式来执行此操作。