"numberFormat" is not a Function, but she is

Asked

Viewed 46 times

1

Sirs,

The situation is as follows. I got a function to format numbers for financial formats. When applying, it says it is not a function. But it is.

How to proceed?

<script>

    // * Função para formatar em formato de dinheiro
    Number.prototype.numberFormat = function(n, x, s, c) {
        var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')',
            num = this.toFixed(Math.max(0, ~~n));

        return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ','));
    };




    // * Aqui que vou utiliza-la
    $(function(){

        ...

        // * Função que busca o plano mais adequado no BD
        function atualizaPlano(disco, ram, cpu, os){

            $.ajax({

                ...

                // * Caso retorne
                success:function(produto){

                    valorProduto = produto[0]['proValor'];
                    valorProduto = valorProduto.numberFormat(2, 3, '.', ',');

                    ...

                },

                ...

            });

        }

    });

</script>
  • Which way out of console.log(typeof valorProduto )?

  • 1

    Probably valorProduto is as string, and numberFormat exists only for numbers

  • @Arturotemplário probably was that same... I forced with parseFloat and it worked!

1 answer

3


Try:

valorProduto = parseFloat(produto[0]['proValor']);
  • Perfect! That’s right. I have to force the business type so when to always use...

Browser other questions tagged

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