Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

323
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!