Float field conversion problems

Asked

Viewed 72 times

0

I have a problem that at first, is silly, but I am not able to solve, I have this function in ajax, that takes the values of input, and sends it to the controler:

 var url = "/PedidoFornecedor/Adicionar1";
        var ProdutoPedidoID = $idprodutopedido;
        var ProdutoID = $("#produtoid").val();
        var CodigoProduto = $("#idproduto").val();
        var Qtd = $("#qtd").val();
        var QtdFalta = $("#qtd").val();
        var PrecoCusto = $("#precocusto").val();
        var DescontoV = $("#descontov").val();
        var DescontoP = $("#descontop").val();
        var ICMS = $("#icms").val();
        var AliquotaICMS = $("#aliquotaicms").html().replace("%", "");
        var IPI = $("#ipi").val();
        var ISS = $("#iss").val();
        var PedidoID = $("#pedidoid").val();
        var DataEntrega = $("#dataentrega").val();
        var DescricaoProduto = $("#descricaoproduto").html();
        var Total = (parseFloat(Qtd) * parseFloat(PrecoCusto.replace(/\./g, '').replace(',', '.'))) - parseFloat(DescontoV.replace(/\./g, '').replace(',', '.'));
        var vICMS = (parseFloat(Total) * (parseFloat(AliquotaICMS.replace(/\./g, '').replace(',', '.')) / 100));
        var vISS = (parseFloat(Total) * (parseFloat(ISS.replace(/\./g, '').replace(',', '.')) / 100));
        var vIPI = (parseFloat(Total) * (parseFloat(IPI.replace(/\./g, '').replace(',', '.')) / 100));

        $.ajax({
            url: url,
            data: {
                produtoPedidoID: ProdutoPedidoID, produtoID: ProdutoID, codigoProduto: CodigoProduto, qtd: Qtd, qtdFalta: QtdFalta, precoCusto: PrecoCusto,
                descontoV: DescontoV, descontoP: DescontoP, icms: ICMS, aliquotaICMS: AliquotaICMS,
                ipi: IPI, iss: ISS, dataEntrega: DataEntrega, pedidoID: PedidoID, descricaoProduto: DescricaoProduto,
                total: Total, vicms: vICMS, viss: vISS, vipi: vIPI
            },
            datatype: "json",
            type: "POST",
            success: function (data) {

And here is the code to receive on controller:

public ActionResult Adicionar1(int? produtoPedidoID, int produtoID, string codigoProduto, int qtd, int qtdFalta, float precoCusto, float descontoP,
     float descontoV, int icms, float aliquotaICMS, float ipi, float iss, DateTime? dataEntrega,
     int pedidoID, string descricaoProduto, float total, float vicms, float viss, float vipi)
    {

In these variables, however, it never works, every time I try gives problem in some number, if I send 1000,50 it saves 100050, or if I send 5,202,00 it receives without being format, always with problems in any of the types. or the problem in the pennies, or in the tens, I’ve tried several ways, either it works one or it works the other kind, no way I tried it worked 100%. I know it’s silly, I’d like to better understand what’s going on in these conversion problems. I have also tried in the controller receive string type, and also always gives some problem.

EXAMPLE Thus, in the debugger the total amount being sent by ajax is 5300.2, and in the controller he’s getting 53002 and it would be right to receive 5300,20

EDIT I tried that way below and it didn’t work either:

var Total = Number(parseFloat(Qtd) * Number(parseFloat(PrecoCusto.replace(/\./g, '').replace(',', '.')))) - Number(parseFloat(DescontoV.replace(/\./g, '').replace(',', '.')));
        var vICMS = Number(parseFloat(Total) * Number(parseFloat(AliquotaICMS.replace(/\./g, '').replace(',', '.')) / 100));
        var vISS = Number(parseFloat(Total) * Number(parseFloat(ISS.replace(/\./g, '').replace(',', '.')) / 100));
        var vIPI = Number(parseFloat(Total) * Number(parseFloat(IPI.replace(/\./g, '').replace(',', '.')) / 100));
  • Are you using the comma to separate the whole part from the fractionated part? Check who is the decimal point in the two languages you are working on, Javascript usually ignores the location information and uses "." (dot) but I don’t know if in the language of controller (C#? ) is the same.

  • Have you tried using maskMoney? We did an implementation very similar to yours, we used maskMoney for decimal problems.

  • @Adrianopraia never used, has some example to assist ?

  • 1

    Here in the repository is an example of how to use maskMoney, any questions available. https://github.com/plentz/jquery-maskmoney

1 answer

0

Use the function Number.prototype.toFixed() to format a number using fixed point notation.

Example:

console.log(Number.parseFloat("12.872").toFixed(2));
// retorna 12.87

Going back to your code:

//    Vou usar Total, PrecoCusto, DescontoV e Qtd numéricos para simplificar os calculos 
// e a leitura. Então vou cria os campos _Total, _PrecoCusto, _DescontoV e _Qtd
// pois não sei se você fará uso futuro dos valores originais.     
var _PrecoCusto = parseFloat( PrecoCusto.replace(/\./g, '').replace(',', '.') );
var _DescontoV  = parseFloat( DescontoV.replace(/\./g, '').replace(',', '.') );
var _Qtd = parseFloat( Qtd.replace(/\./g, '').replace(',', '.') );
var _AliquotaICMS = parseFloat(AliquotaICMS.replace(/\./g, '').replace(',', '.')) / 100;
var _ISS = parseFloat(ISS.replace(/\./g, '').replace(',', '.')) / 100;
var _IPI = parseFloat(IPI.replace(/\./g, '').replace(',', '.')) / 100;

Total = _Qtd *  _PrecoCusto - _Desconto;
var vICMS = Total * _AliquotaICMS ;
var vISS = Total * _ISS;
var vIPI = Total * _IPI;

//Lá em cima poderia ser feito var Total = (_Qtd *  _PrecoCusto - _Desconto).toFixed(2);
//Mas está dessa forma para simplificar a leitura. O mesmo vale para vICMS, vISS e vIPI 
Total = Total.toFixed(2);
vICMS = vICMS.toFixed(2);
vISS = vISS.toFixed(2);
vIPI = vIPI.toFixed(2);

Browser other questions tagged

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