Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

318
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda