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

153
Visualizações
Display INR number only if it's a number with more than 3 digits

JSFiddle DEMO: https://jsfiddle.net/bukh7jwL/

I have a table with text & numbers and facing 2 problems.

  1. It disables all the text values and displays it as ₹NaN!!
  2. I would like to avoid applying the JS if the number has less than 100 as value or 3 digits.

How do I go about doing this?

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 Respostas
Responde à pergunta

0

I've added the following statement to your code to check if we want to run the NumberFormat.

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

$.isNumeric(monetary_value) checks if it's a number.

(monetary_value < 100 || monetary_value.length == 3) checks if the value is below 100 or the length is 3 characters long.

$('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 Relatório

0

You have to first check if the string is a number or not after that you need to check if the number digits are 3 or more. After that using toLocaleString() the number will be converted into INR currency

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 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