Problem with toFixed in return Json

Asked

Viewed 1,044 times

2

I’m having trouble using toFixed in a return json.

A part of the code

success: function (dados){
                $.each(dados, function(index){
                    var guidPedido              = dados[index].guid;
                    var statusPedido            = dados[index].status;
                    var nomePedido              = dados[index].nome;
                    var enderecoPedido      = dados[index].endereco;
                    var totalPedido             = dados[index].total;
                    var dataPedido              = dados[index].data;
                    var telefonePedido      = dados[index].telefone;
                    var numerocasaPedido    = dados[index].numero;
                    var formaPagamento      = dados[index].formaPagamento;
                    var observacaoPedido    = dados[index].observacao;
                    var cpfClientePedido    = dados[index].cpf;
                    var entregarPedido      = dados[index].entregar;
                    var tokenPedido             = dados[index].token;
                    var bairroPedido            = dados[index].bairro;
                    var pagamentotext       = 0;

                    if (statusPedido == 1) {
                        var statuspedidotext = "Processando";
                    }

                    if (formaPagamento == 0) {
                        pagamentotext = "Dinheiro";
                    } else {
                        pagamentotext = "Cartão/Crédito/Débito";
                    }

                    totalPedido = totalPedido.toFixed(2);

The mistake is:

ERROR: lanc_pedidos.php:112 Uncaught Typeerror: Cannot read Property 'toFixed' of null

After withdrawing the possibility of dados[index].total was null, get the following error:

Typeerror: totalPedido.toFixed is not a Function

Can someone explain to me what’s wrong ?

  • That mistake means that dados[index].total is null. This value may be null? if "yes" you want to convert null to zero? case "no" you have an error somewhere else you need to fix first

  • right now presents this Typeerror: totalPedido.toFixed is not a Function

  • Okay, what do you give console.log(typeof dados[index].total, dados[index].total);?

  • I got Sérgio, as follows totalPedido = parseFloat(totalPedido). toFixed(2);

  • Okay, you were getting a string then. Great. If you already solved it you can delete the question maybe... or answer.

  • thanks for the help Sérgio, I will respond and leave in case someone has the same problem

  • If you’re going to answer, state in detail Type of the variable, and why the error appeared and what makes the parseFloat:)

Show 2 more comments

2 answers

2

The problem was that the returned value was a string

TypeError: totalPedido.toFixed is not a function

convert solution to float

totalPedido = parseFloat(totalPedido).toFixed(2); 

2


The problem is that the .toFixed() is a method for converting numbers into strings. That is a method for type variables Number. The mistake it makes tells me you’re getting one String.

Take a look at the example:

var totalPedido = '10';
totalPedido.toFixed(2); 

This makes the mistake you saw:

VM329:1 Uncaught Typeerror: totalPedido.toFixed is not a Function(...)

If your variable had one Number that didn’t happen anymore:

var totalPedido = 10;
totalPedido.toFixed(2); // dá "10.00"

So first you have to convert your text with numbers inside (type String) to a number and then limit the decimal places by converting back to String.

To convert to number you can use the Number() or the parseFloat. So your code could be:

var totalPedido = Number(dados[index].total).toFixed(2);

Browser other questions tagged

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