convert integer to decimal in jquery

Asked

Viewed 2,067 times

4

I need to convert for example the value "025" for "0.25" or "005" for "0.05":

I’m trying to do it this way:

var valor1 = $("#ValorDesconto").val().replace(/[^\d]+/g,'');
parseFloat(valor1).toFixed(2)

How do I ? the way I did above is always added two zeros to the right: for example: of "025" for "25.00" or "005" for "5.00".

Here is the full code:

var valor1 = $("#ValorUnitario").val().replace(/[^\d]+/g,'');
var valor2 = $("#ValorDesconto").val().replace(/[^\d]+/g,'');

                    var _produto = {
                        "PaoID": $("#SelPao").val(),
                        "FermentacaoID": $("#SelFermentacao").val(),
                        "Observacao": $("#Observacao").val(),
                        "Status": true,
                        "ValorUnitario": Number(valor1).toFixed(2),
                        "ValorDesconto": Number(valor2).toFixed(2)
                    };
                    console.log(_produto);

                    $.ajax({
                        url: "/Administrativo/Produto/SalvarJSON",
                        type: "POST",
                        dataType: "JSON",
                        contentType: "application/json charset=utf-8",
                        processData: false,
                        data: JSON.stringify(_produto),
                        beforeSend: function () {
                        },
                        complete: function () {
                        },
                        success: function (data) {

                            if (data.ok == true) {
                                window.location.href = "/Administrativo/Produto/Index";
                            } else {
                                $("#advertenciaModal .modal-body").html("<div>" + data.msg + "</div>");
                                $("#advertenciaModal").modal();
                            };

                        },
                        error: function (result) {
                            window.location.href = "/Administrativo/Produto/Index";
                        }
                    });

I used Number(valor1).toFixed(2) as suggested by Otto but without effect

inserir a descrição da imagem aqui

3 answers

3

You could do it this way

Number(1).toFixed(2);  
  • I edited the question with the suggestion and it didn’t work.

  • @Adrianosuv funny that in the print appears right, the error is of an image that is missing.

  • Hello @Otto the way you posted there is no error, the problem is that it does not convert the way you want if you look at the print we have for example Valordesconto = "25,00" I implemented the Number function and typed in the input "0,25" and converted to "25,00".

2


You can use the method substring from Javascript to format

var valor = "025";
var len = valor.length;
var valorFormatado = valor.substring(0, len - 2) + "." + valor.substring(len - 2);
console.log(valor);
console.log(valorFormatado);

Explaining:

  • valor.substring(0, len - 2): takes a string from the index 0 up to two before the last.
  • + "." +: concatenates a point between the two strings.
  • valor.substring(len - 2): gets the last two digits.
  • valorFormatado: will have the string with a point before the last two digits.

In sequence, you can convert to float/decimal using parseFloat(valorFormatado)

  • It worked, thank you!

2

If at this value the last two numbers are always the decimal places then the simplest would be divided by 100

var _produto = {
    "PaoID": $("#SelPao").val(),
    "FermentacaoID": $("#SelFermentacao").val(),
    "Observacao": $("#Observacao").val(),
    "Status": true,
    "ValorUnitario": parseInt(valor1) / 100,
    "ValorDesconto": parseInt(valor2) / 100
};

This way the value is getting as number even in the object, which is usually ideal, but depending on your need may have to convert to string as well

  • Dividing by 100 is much simpler, I hadn’t even thought about it. + 1

  • Good! , really I’ll test too.

Browser other questions tagged

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