Javascript 如何将我用逗号替换点的数字从字符串转换回十进制数字
我有一个输入字段,用户可以使用点或逗号输入十进制数(例如:79,99或79.99)。当用户用逗号输入数字时,我将其替换为点(然后将该数字发送到远程服务器进行验证)。现在,我想在用户使用toFixed(2)函数在页脚中输入数字后显示数字,以仅显示前两个十进制数字。我无法让它工作,因为在使用ParseFloat将其转换回数字后,逗号消失后的2位小数 例如: 用户在输入字段中输入:79,99,然后将其设置为变量totalAmount:Javascript 如何将我用逗号替换点的数字从字符串转换回十进制数字,javascript,jquery,replace,number-formatting,parseint,Javascript,Jquery,Replace,Number Formatting,Parseint,我有一个输入字段,用户可以使用点或逗号输入十进制数(例如:79,99或79.99)。当用户用逗号输入数字时,我将其替换为点(然后将该数字发送到远程服务器进行验证)。现在,我想在用户使用toFixed(2)函数在页脚中输入数字后显示数字,以仅显示前两个十进制数字。我无法让它工作,因为在使用ParseFloat将其转换回数字后,逗号消失后的2位小数 例如: 用户在输入字段中输入:79,99,然后将其设置为变量totalAmount: $('#amount-1').keyup(function() {
$('#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");
}
}
$('#amount-1').keyup(函数(){
GetReturnAmount(1);//此函数调用calcTotalAmount函数
})
函数calcTotalAmount(){
console.log('calcTotalAmount');
var totalAmount=0;
var=0;
var rowAmount=0;
var=0;
对于(变量i=1;i<4;i++){
rowAmount=parseFloat($('#amount-'+i).val());
RowReturn=parseFloat($('#结果-'+i).text());
如果(!isNaN(行金额)){
合计金额+=行金额;
全额退款+=全额退款;
}
}
var toPay=总退款金额;
totalAmount=totalAmount.toString().replace(/\,/g,'.');
totalAmount=parseFloat(totalAmount).toFixed(2);
$(“#页脚退款”).text(全额退款)
$(“#页脚合计”).text(合计金额)
如果(总金额=“”){
$('页脚总计').text(“0.00”);
}
}
页脚中显示的结果是:79.00而不是79.99我在JSFIDLE中尝试了您的代码,但在“.toFixed(2)”上出错 稍加修改后,我就可以避免错误了
$(function() {
var totalAmount = '79,99';
totalAmount = parseFloat( totalAmount.toString().replace(/\,/g, '.'));
totalAmount = totalAmount.toFixed(2);
$('#footer-refund').text(totalAmount)
});
我在JSFIDLE中尝试了您的代码,但在“.toFixed(2)”上出错 稍加修改后,我就可以避免错误了
$(function() {
var totalAmount = '79,99';
totalAmount = parseFloat( totalAmount.toString().replace(/\,/g, '.'));
totalAmount = totalAmount.toFixed(2);
$('#footer-refund').text(totalAmount)
});
看起来您提供的代码工作正常,但您可以应用一些修复程序:
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
顺便说一句,你需要
$(“#页脚退款”).text(totalreturn.toFixed(2))
,不是应该是$(“#页脚退款”).text(totalAmount)
(不确定你为什么在这里也使用toFixed)?看起来你给出的代码工作得很好,但有一些修复方法可以应用:
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
顺便说一句,你需要
$(“#页脚退款”).text(totalreturn.toFixed(2))
,不是应该是$(“#页脚退款”).text(totalAmount)
(不确定你为什么在这里也使用toFixed)?感谢所有的帮助:
解决方案如下:我必须在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;
}
}
}
for(变量i=1;i<4;i++){
if(!$('#amount-'+i).val(){continue;}else{
rowAmount=parseFloat($('#amount-'+i).val().toString().replace(/\,/g');
RowReturn=parseFloat($('#结果-'+i).text());
如果(!isNaN(行金额)){
合计金额+=行金额;
全额退款+=全额退款;
}
}
}
感谢您的帮助:
解决方案如下:我必须在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;
}
}
}
for(变量i=1;i<4;i++){
if(!$('#amount-'+i).val(){continue;}else{
rowAmount=parseFloat($('#amount-'+i).val().toString().replace(/\,/g');
RowReturn=parseFloat($('#结果-'+i).text());
如果(!isNaN(行金额)){
合计金额+=行金额;
全额退款+=全额退款;
}
}
}
我们可以看看您的输入字段吗?由于您使用了.toString(),我认为您将输入设置为数字,但是79,99(带逗号)不是一个可识别的数字。也许数字(totalAmount)会执行此操作错误在其他地方,此代码对我来说很好。是的,在我使用输入type=“text”的测试中,代码对我来说非常好,然后执行:console.log(parseFloat(document.getElementById(“test”).value.toString().replace(/,/,“))是您在最后一行中使用的变量TotalReturn应该与totalAmount具有相同的值吗?我假设您复制粘贴了代码,请注意,您在此处使用了两个不同的变量。我们可以查看您的输入字段吗?既然您使用了.toString()我想您已经将输入设置为number,但是79,99(带逗号)不是一个可识别的数字。也许number(totalAmount)会做一些小动作。错误在其他地方,这段代码对我来说很好。是的,在我的测试中,代码对我来说非常好,输入type=“text”,然后执行:console.log(parseFloat(document.getElementById(“test”).value.toString().replace(/,/,“))您在最后一行中使用的变量TotalReturn是否应该与totalAmount具有相同的值?我假设您对代码进行了复制粘贴,请注意,您在此处使用了两个不同的变量,这是一个太多的变量。仍然需要限制它。如果您在
中使用
,您可以使用类似的内容:
尽管如此,您需要记住,myregexhere
与常规javascriptYea中的/myregexhere/g
相当,这太多了。但我还是需要限制它。如果您在
中使用
可以这样使用:
但是,您需要记住myregexhere
相当于普通Javascript中的/myregexhere/g
,似乎在小提琴中工作,只是我不,我会发布更多的代码,也许我做错了什么…似乎在小提琴中工作,只是我不,我会发布更多的代码,也许我做错了什么。。。