convert integer to decimal in jquery

4

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

I'm trying to do it like this:

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: example: from "025" to "25.00" or "005"

Here is the complete 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 Otto's suggestion but no effect

    
asked by anonymous 29.09.2016 / 21:40

3 answers

2

You can use the Javascript substring 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) : gets a string from index 0 to two before the last.
  • + "." + : concatenates a period between the two strings.
  • valor.substring(len - 2) : gets the last two digits.
  • valorFormatado : will have the string with a period before the last two digits.

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

    
29.09.2016 / 22:08
3

You could do it this way

Number(1).toFixed(2);  
    
29.09.2016 / 21:46
2

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

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

In this way the value is getting like number even in the object, which is usually ideal, but depending on your need you might have to convert to string as well

    
29.09.2016 / 22:20