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

80
Visualizações
Improve javascript number rounding

I was wondering if there's a way to improve and make this function faster, less resource consuming and more readable:

calculateMerchantEarnings(original_price) {
    const processor_percentage = Math.round((original_price * 0.029 + Number.EPSILON) * 100) / 100;
    const processor_flat = 30; // in cents
    const myfees_and_processing_fees = processor_percentage + processor_flat + 100;
    const final_price_minus_deductions = original_price - myfees_and_processing_fees;
    const final_price_in_cents = Math.round((final_price_minus_deductions + Number.EPSILON) * 100) / 100;
    return (
      Math.round((final_price_in_cents / 100 + Number.EPSILON) * 100) / 100
    );
  }

What I do is:

  1. Pass the original_price in cents
  2. Calculate CC Processor percentage fees
  3. Add my fees ($1) and the processor's percentage and flat fee
  4. calculate the final price by deducting the total fees from the original_price
  5. Round up the final price in cents
  6. Convert the final price to dollars and round up

I have a feeling I may be able to do all this with one line :)

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

0

Basically, you could add all into a single expression.

function calculateMerchantEarnings(original_price) {
    const processor_percentage = Math.round((original_price * 0.029 + Number.EPSILON) * 100) / 100;
    const processor_flat = 30; // in cents
    const myfees_and_processing_fees = processor_percentage + processor_flat + 100;
    const final_price_minus_deductions = original_price - myfees_and_processing_fees;
    const final_price_in_cents = Math.round((final_price_minus_deductions + Number.EPSILON) * 100) / 100;
    return Math.round((final_price_in_cents / 100 + Number.EPSILON) * 100) / 100;
}

const fn = x => Math.round(x * 0.971 - 130) / 100;

console.log(calculateMerchantEarnings(1000));
console.log(fn(1000));

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