Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

128
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda