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

230
Visualizações
Add a symbol as suffix after detecting a number

I would like to add suffix to the number with % instead of the current ₹ symbol in the front and also remove the .00 decimal points too. So the result shows as 9% instead of ₹9.00

I am not sure if one could modify this script to suit my needs. Here's my code:

$('.percent').each(function() {
  var monetary_value = $(this).text();
  
  if ($.isNumeric(monetary_value) && (monetary_value < 100 || monetary_value.length == 2)) {
    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.6.0/jquery.min.js"></script>
<div class="percent">9</div>

Demo JSFiddle: http://jsfiddle.net/4p98aw6u/

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

To do what you require simply remove the usage of Intl.NumberFormat() and instead append the % character to the end of the value.

Also note the explicit conversion of the val() to an integer using parseInt() before being used in any comparisons.

$('.percent').each(function() {
  var monetary_value = parseInt($(this).text(), 10);
  var i;
  
  if (monetary_value && (monetary_value < 100 || monetary_value.length == 2)) {
    i = monetary_value + '%';
  }
  
  $(this).text(i);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="percent">9</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

A regular expression might be the simplest way to do the job, depending on the input

const monetary_value = $(this).text(); // ₹9.00
const converted_value = monetary_value.match(/\d+?./)[1] + "%"

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match particularly since you should add some error checking to the above for when the match is null

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