Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
Muestra el número INR solo si es un número con más de 3 dígitos

DEMOSTRACIÓN JSFiddle: https://jsfiddle.net/bukh7jwL/

Tengo una tabla con texto y números y me enfrento a 2 problemas.

  1. ¡Deshabilita todos los valores de texto y los muestra como ₹NaN!
  2. Me gustaría evitar aplicar el JS si el número tiene menos de 100 como valor o 3 dígitos.

¿Cómo voy a hacer esto?

HTML:

 <center> <p> <table> <tr><td>This is amazing</td></tr> <tr><td>92834</td></tr> <tr><td>33</td></tr> <tr><td>What?</td></tr> </table> </center>

JS:

 $('td').each(function() { var monetary_value = $(this).text(); var i = new Intl.NumberFormat('en-IN', { style: 'currency', currency: 'INR' }).format(monetary_value); $(this).text(i); });
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Agregué la siguiente declaración a su código para verificar si queremos ejecutar NumberFormat .

$.isNumeric(monetary_value) && (monetary_value < 100 || monetary_value.length == 3)

$.isNumeric(monetary_value) comprueba si es un número.

(monetary_value < 100 || monetary_value.length == 3) comprueba si el valor está por debajo de 100 o si la longitud es de 3 caracteres.

 $('td').each(function() { var monetary_value = $(this).text(); if ($.isNumeric(monetary_value) && (monetary_value < 100 || monetary_value.length == 3)) { var i = new Intl.NumberFormat('en-IN', { style: 'currency', currency: 'INR' }).format(monetary_value); } $(this).text(i); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <center> <p> <table> <tr><td>This is amazing</td></tr> <tr><td>92834</td></tr> <tr><td>33</td></tr> <tr><td>What?</td></tr> </table> </center>

about 4 years ago · Juan Pablo Isaza Report

0

Primero debe verificar si la cadena es un número o no, luego debe verificar si los dígitos del número son 3 o más. Después de eso, usando toLocaleString() el número se convertirá a la moneda INR

 function isNumeric(n) { return !isNaN(parseFloat(n)) && isFinite(n); } $('td').each(function() { var monetary_value = $(this).text(); if(isNumeric(monetary_value)) { let moneyVal = parseFloat(monetary_value); if( (moneyVal < 100 || moneyVal.toString().length == 3) ){ var i = moneyVal.toLocaleString('en-IN', { maximumFractionDigits: 2, style: 'currency', currency: 'INR' }); $(this).text(i); } } });
 <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <center> <p> <table> <tr><td>This is amazing</td></tr> <tr><td>92834</td></tr> <tr><td>33</td></tr> <tr><td>What?</td></tr> </table> </center>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!