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

152
Views
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 answers
Answer question

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 Report

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