jQuery 中的千位分隔符导致 parseFloat 误解数字

Thousands separator in jQuery is causing parseFloat to misinterpret numbers

我使用 . 作为千位分隔符。 所以字符串 128.814 应该读作 "one hundred and twenty eight thousand, eight hundred and fourteen"。

当我把它加到数字 900 上时,我希望答案是 129714(显示为 129.714)。 但是,因为 javascript 将千位分隔符作为小数点,所以它给了我一个不希望的结果。

function addTotalPrice(item){
    var priceItem = parseFloat( $(item).find('.price').text() );
    var priceTotal = parseFloat( $('.label-price span').text() );

    var result = (priceTotal + priceItem);
    $('.label-price span').text( result.toFixed(3) );
}

如何告诉 jQuery/Javascript 将 128.814 中的 . 解释为千位分隔符而不是小数点?

How can I tell jQuery/Javascript to interpret the . in 128.814 as a thousands separator and not a decimal point?

最简单的方法是将其删除。

当您删除它时,它会自动被解释为千值,即使那里没有逗号。然后,手动格式化数字,如 How to print a number with commas as thousands separators in JavaScript

function addTotalPrice(item){
    var priceItem = parseFloat( $(item).find('.price').text().replace('.','') );
    var priceTotal = parseFloat( $('.label-price span').text().replace('.','') );

    var result = (priceTotal + priceItem);
    $('.label-price span').text( result.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "."); );
}

var priceItem = parseFloat('900'.replace('.', ''));
var priceTotal = parseFloat('128.814'.replace('.', ''));

var result = (priceTotal + priceItem);
var fixedResult = result.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
document.write(fixedResult);