Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
Round up the cents field in JavaScript

Here is the situation, I'm entering a dollar amount within the text field something similar to the following code:

<input type="text" name="qtr-revenue-<?php echo $qtr ?>" id="qtr-revenue-<?php echo $qtr ?>" class="qtr-revenue editable" value="<?php echo App_Number::formatCurrency($qtrNumbers['Revenue'], 2, '', true, '') ?>" />

However, the following thing is happening, I'm able to properly format for example 9,000,000.90 with no issues, however, whenever I mistakenly enter 9,000,000.90000099323, it formats the number to 9,000,000.900,000,993,23

How do I properly format the dollar amount from 9,000,000.90000099323 to 9,000,000.90?

Here is the javascript code that formats the number right now.

$(this).val(function (index, value) {
    return value
        .replace(/[^-\d\.]/g, "")
        .replace(/\B(?=(\d{3})+(?!\d))/g, ",");
});
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can try this approach. It's not supported in IE11 though. Also you can play around with different currencies and their formats.

Here is some guides: MDN Web Docs

const number = "9,000,000.90000099323".replace(/,/g, '');
console.log(new Intl.NumberFormat('en-CA', { minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(number));

So in your case it could be something like

$(this).val(new Intl.NumberFormat('en-CA', { minimumFractionDigits: 2, maximumFractionDigits: 2 }).format("9,000,000.90000099323".replace(/,/g, '')));
about 4 years ago · Juan Pablo Isaza Relatório

0

Update, I found a much simpler solution:

If I post the following code:

           if (revenue.toString().indexOf('.') != -1) {
                $(this).val(revenue.toFixed(2));
            } 

This does the job that I'm looking for.

about 4 years ago · Juan Pablo Isaza Relatório

0

Not an JS expert but I have a financial website and to make a number look like the following I use the code below 1,139,447,000

String(value).replace(/(.)(?=(\d{3})+$)/g,'$1,');

I believe you should be able to change the result by updating the regex in here

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda