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

180
Visualizações
Math.floor inconsistently truncating to 2 decimal places - Javascript/Acrobat

I know similar questions get asked here frequently, but I've not been able to find an answer that I've been able to make work after searching for a couple hours; could really use your help.

Working on a pricing calculator using Javascript in Adobe Acrobat and I need to limit a number to exactly 2 decimal places without any rounding.

I need to limit the decimal places because the next step in the calculator is to adjust that number to fit in our pricing structure based on where its decimals fall within a range. Here's my adjustment code for some context.

if (decimals < 0.20)event.value = -1.01;
else if ((decimals > 0.19) && (decimals < 0.30))event.value = 0.29;
else if ((decimals > 0.29) && (decimals < 0.40))event.value = 0.39;
else if ((decimals > 0.39) && (decimals < 0.50))event.value = 0.49;
else if ((decimals > 0.49) && (decimals < 0.60))event.value = 0.59;
else if ((decimals > 0.59) && (decimals < 0.70))event.value = 0.69;
else event.value = 0.99;

The problem arises when I have a value of, for example, 15.390001. I need that adjustment code to return 15.39, but right now it's being pushed to 15.49, since x.390001 is greater than x.39.

Currently using the code below to truncate a number to 2 decimal places without rounding.

var flcost = this.getField("fl_cost"); // Front Line Cost
var multi = this.getField("multiplier_calc"); // Subcat Multiplier
var dep = this.getField("deposit"); //Deposit total

var nAdj = Math.pow(10, 2); // decimal adjustment

var rough = Math.floor(flcost.value * multi.value * nAdj) / nAdj;
event.value = rough+deposit;

I'm getting inconsistent values returned. For instance, it will correctly truncate 11.376 down to 11.37, and 9.432 down to 9.43.

However, when I feed it something like 23.832, it's returning 23.830000000000002, and in another case, when fed 143.832, it's returning 143.82999999999998. In other cases I've seen it actually rounding up, so I'm just incredibly confused.

So, what do I need to do to properly cut a number down to 2 decimal places without rounding?

And sorry for potentially over-explaining things!

about 4 years ago · Juan Pablo Isaza
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