【问题标题】:How to convert a number where I replaced the dot with a comma from a string back to a decimal number如何将我用逗号替换点的数字从字符串转换回十进制数字
【发布时间】:2017-08-22 15:18:25
【问题描述】:

我有一个输入字段,用户可以在其中使用点或逗号输入十进制数字(例如:79,99 或 79.99)。当用户用逗号输入数字时,我将其替换为点(然后将该数字发送到远程服务器进行验证)。现在我想在用户使用 toFixed(2) 函数在页脚中输入数字后显示数字,以仅显示前两个十进制数字。我无法让它工作,因为在使用 ParseFloat 将其转换回数字后,逗号消失后的 2 位小数。

例子:

用户在输入字段中输入:79,99,然后将其设置为变量 totalAmount:

$('#amount-1').keyup(function() {

getRefundAmount(1); // This function calls the calcTotalAmount function

})

function calcTotalAmount() {
console.log('calcTotalAmount');

var totalAmount = 0;
var totalRefund = 0;
var rowAmount = 0;
var rowRefund = 0;

for(var i = 1; i < 4; i++ ) {
    rowAmount = parseFloat($('#amount-' + i).val());
    rowRefund = parseFloat($('#result-' + i).text());
    if(!isNaN(rowAmount)) {
        totalAmount += rowAmount;
        totalRefund += rowRefund;
    }
}

var toPay = totalAmount-totalRefund;
totalAmount = totalAmount.toString().replace(/\,/g, '.');
totalAmount = parseFloat(totalAmount).toFixed(2);


$('#footer-refund').text(totalRefund)
$('#footer-total').text(totalAmount)
if (totalAmount == '') {
    $('#footer-total').text("0.00");
}
}

页脚中显示的结果是:79.00 而不是 79.99

【问题讨论】:

  • 我们可以看看您的输入字段吗?由于您执行 .toString() 我认为您将输入设置为数字,但 79,99(带逗号)不是可识别的数字。
  • 也许 Number(totalAmount) 可以解决问题
  • 错误在其他地方,这段代码对我来说很好。
  • 是的,在我的测试中,输入 type="text" 的代码对我来说工作得非常好,然后执行:console.log(parseFloat(document.getElementById("test").value.toString ().replace(/,/, ".")))
  • 您在最后一行中使用的变量 totalRefund 是否应该与 totalAmount 具有相同的值?我假设你复制粘贴了你的代码,请注意你在这里使用了两个不同的变量

标签: javascript jquery replace number-formatting parseint


【解决方案1】:

我在 jsfiddle 中尝试了您的代码,但在“.toFixed(2)”上出现错误 稍作更改后,我可以防止错误。:

$(function() {
   var totalAmount = '79,99';
   totalAmount = parseFloat( totalAmount.toString().replace(/\,/g, '.'));
   totalAmount = totalAmount.toFixed(2);
   $('#footer-refund').text(totalAmount)
});

https://jsfiddle.net/jkrielaars/2gb59xss/1/

【讨论】:

  • 似乎在小提琴中工作,只是我没有,我会发布更多代码,也许我做错了什么......
【解决方案2】:

您提供的代码似乎运行良好,但您可以应用一些修复:

totalAmount = ("" + totalAmount).replace(/\,/g,'.');
totalAmount = parseFloat(totalAmount);
//use toFixed only if you don't restrict input to 2 decimals, but I'd recommend restricting input otherwise you'll encounter rounding issues

顺便说一句,你是$('#footer-refund').text(totalRefund.toFixed(2)),不应该是$("#footer-refund").text(totalAmount)(不知道你为什么在这里也使用toFixed)?

【讨论】:

  • 是的,这太过分了。我仍然需要限制它。
  • 如果你在&lt;form&gt; 中使用&lt;input&gt;,你可以使用类似这样的东西:&lt;input [insert thingies here] pattern="myregexhere"&gt; 虽然,你需要记住myregexhere 相当于@987654328 @ 在常规 javascript 中
【解决方案3】:

感谢大家的帮助:

解决方案如下:我必须在for循环中检查它并在那里替换它。

for(var i = 1; i < 4; i++ ) {
    if(!$('#amount-' + i).val()){continue;}else{ 
    rowAmount = parseFloat($('#amount-' + i).val().toString().replace(/\,/g,'.'));
    rowRefund = parseFloat($('#result-' + i).text());
    if(!isNaN(rowAmount)) {
        totalAmount += rowAmount;
        totalRefund += rowRefund;
    }
    }
}

【讨论】:

    猜你喜欢
    • 2015-09-20
    • 2017-05-30
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 2014-03-19
    相关资源
    最近更新 更多