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

327
Visualizações
JavaScript / reactJS - round up last decimal (without hardcoded decimal to calculate)

By using JavaScript to calculate, For Example (in live can be any value)

1000/0.997 = 1003.0090270812437

By using PC calculator to calculate

1000/0.997 = 1003.009027081244 // auto round up last decimal

enter image description here

But if !!! this is correct and same as window, in this situation no need round up

500/0.997 = 501.5045135406219 // this is correct and same as window calculator

enter image description here

My prefer output is the auto round up value 1003.009027081244

Question: In JavaScript, how to round up the last digit of decimal?

What I have tried:

Math.round(1003.0090270812437) // 1003
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Option 1: playing with numbers and decimals

const countDecimals = value => {
  let text = value.toString()
  if (text.indexOf('e-') > -1) {
    let [base, trail] = text.split('e-')
    let deg = parseInt(trail, 10)
    return deg - 1
  }

  if (Math.floor(value) !== value) return value.toString().split(".")[1].length - 1 || 0

  return 0
}
const round = number => Math.round(number * Math.pow(10, countDecimals(number - 1))) / Math.pow(10, countDecimals(number - 1))

Option 2: playing with strings

let round = number => {
  let str = (number).toString()
  let [num, dec] = str.split(".")
  let leading_zeros = ""
  for (const char of dec) {
    if (char !== '0') break
    leading_zeros += '0'
  }
  dec = Math.round(parseFloat(dec.substring(0, dec.length - 1) + "." + dec[dec.length - 1])).toString()
  return parseFloat(num + "." + leading_zeros + dec)
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Maybe you could use string based solution

Convert a decimal number to string, then split it by the .. Then take a decimal part length substracted by 1, and use it in toFixed on the original number.

You will probably need some additional checks, to see if there is a decimal part of a number

const decimal = 1003.0090270812437;
const text = decimal.toString().split('.')[1];

console.log(decimal.toFixed(text.length - 1)); // 1003.009027081244 

about 4 years ago · Juan Pablo Isaza Relatório

0

const result = Number((1000/0.997).toPrecision(16))

const result = Number((1000 / 0.997).toPrecision(16))
console.log(result)

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