2015-04-01 25 views
1

我想了解如何使用parseFloat將字符串轉換爲數字 - 通過這種方式根據用戶獎勵俱樂部點的變化。試圖瞭解如何使用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」等的數字?

這裏是我的小提琴鏈接:http://jsfiddle.net/bkmills1/60vdfykq/

仍在學習...

提前感謝!

+1

如果你要做很多貨幣/數字解析使用庫可能是有益的。像這樣[一個](http://openexchangerates.github.io/accounting.js/)可能會有所幫助。 – Jack 2015-04-01 20:06:57

回答

0

在你的小提琴,變線4來自:

var strParse = parseInt(ptsBalance, 10); 

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

您也可能要刪除所需的任何其他符號,甚至可以使用正則表達式來做到這一點。

+0

是的,它做到了!謝謝盧卡斯! – Brion 2015-04-02 13:40:46

+0

我的榮幸,祝你的項目Scott好運。 – Lukas 2015-04-02 23:55:06

相關問題