Values calculated in Javascript do not appear with decimals

Asked

Viewed 136 times

2

I’ve been doing this calculation, which is working perfectly. The problem that is occurring is that when you return the values to the screen (View) returns without the calculated decimal places.

I’ll demonstrate what’s going on. This is my Javascript:

var QtdeDiasJuros = $("#T_FrmRecTotalDiaAtraso").val();
var TaxaJurosAoDia = $("#T_FrmRecTaxaJurosAoDia").val().replace(",", ".");
var ValorParcela = $("#T_FrmRecValorDuplicata").val();
var TotalJurosAoDia = parseFloat(QtdeDiasJuros) * parseFloat(TaxaJurosAoDia);
var TotalValorPago = (parseFloat(TotalJurosAoDia) + parseFloat(ValorParcela));

document.getElementById("T_FrmRecTotalJurosAoDia").value = TotalJurosAoDia;
document.getElementById("T_FrmRecValorPago").value = parseFloat(TotalValorPago);

Process of Calculation and its result: Exemplifying Calculation with Values Release:

var QtdeDiasJuros = $("#T_FrmRecTotalDiaAtraso").val();
10
var TaxaJurosAoDia = $("#T_FrmRecTaxaJurosAoDia").val().replace(",", ".");
0,333
var ValorParcela = $("#T_FrmRecValorDuplicata").val();
100,50
var TotalJurosAoDia = parseFloat(QtdeDiasJuros) * parseFloat(TaxaJurosAoDia);
3,33
var TotalValorPago = (parseFloat(TotalJurosAoDia) + parseFloat(ValorParcela));
                               103,83  =                            3,33              +     100,50

The problem is that when returning to the screen returns values without decimal place and not positioning as the calculation generated. type: Totalvalorpago fica 10333,00 - Totaljurosaodia fica: 333,00

Here is my View: Here are the fields where you receive these values generated by Javascript

<div class="span2">
    @Html.LabelFor(model => model.T_FrmRecTaxaJurosAoDia)
    @Html.TextBoxFor(model => model.T_FrmRecTaxaJurosAoDia, new { style = "width: 80px; font-weight: bold;  font-size: 18px; color: #39395F; background-color:#E0E0E0 ;  text-align: right;" })
    @Html.ValidationMessageFor(model => model.T_FrmRecTaxaJurosAoDia)
</div>

<div class="span2">
    @Html.LabelFor(model => model.T_FrmRecTotalDiaAtraso)
    @Html.TextBoxFor(model => model.T_FrmRecTotalDiaAtraso, new { style = "width: 100px; font-weight: bold;  font-size: 18px; color: #39395F; background-color:#E0E0E0 ;  text-align: right;" })
    @Html.ValidationMessageFor(model => model.T_FrmRecTotalDiaAtraso)
</div>

<div class="span2">
    @Html.LabelFor(model => model.T_FrmRecTotalJurosAoDia)
    @Html.TextBoxFor(model => model.T_FrmRecTotalJurosAoDia, new { style = "width: 100px; font-weight: bold;  font-size: 18px; color: #39395F; background-color:#E0E0E0 ;  text-align: right;" })
</div>

<div class="span2">
    @Html.LabelFor(model => model.T_FrmRecValorPago, new { style = "font-weight: bold;" })
    @Html.TextBoxFor(model => model.T_FrmRecValorPago, new { style = "width: 200px; font-size: 20px; color: #F2F2F4; background-color:#39395F; text-align: right;" })
</div>
</div>
  • Obrg Pablo, for the correction, I will give myself better, grateful

1 answer

0

I managed to solve the problem of decimal places, that way:

document.getElementById("T_FrmRecTotalJurosAoDia").value = parseFloat(TotalJurosAoDia).toFixed(2).replace(".", ",");
                document.getElementById("T_FrmRecValorPago").value = parseFloat(TotalValorPago).toFixed(2).replace(".", ",");

I hope I can help anyone who might have this same doubt.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.