0

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
FeelRightz
  • 2,777
  • 2
  • 38
  • 73

5 Answers5

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

const result = Number((1000 / 0.997).toPrecision(16))
console.log(result)
DecPK
  • 24,537
  • 6
  • 26
  • 42
anmatika
  • 1,581
  • 3
  • 21
  • 29
0

There is no nice javascript way that I know to achieve this. Due to my knowledge this is the way to go:

Math.round(1003.0090270812437*1000000000000)/1000000000000

Edit: If you do not want to round to a fixed number of decimals, then I would write a little function to count the given decimals before to know your exponent of 10:

var countDecimals = function(value) {
    if (Math.floor(value) !== value)
        return value.toString().split(".")[1].length || 0;
    return 0;
}

var temp = Math.pow(10, countDecimals-1);
Math.round(1003.0090270812437*temp)/temp;
Tom
  • 89
  • 6
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)
}
Dimitar
  • 1,148
  • 8
  • 29
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 
Terminat
  • 1,199
  • 5
  • 21
-1

You can just use toFixed(${Amount of decimals you want to show})

Example: (2321/912).toFixed(1) will show one fraction of the decimal number

(2322/ 1232).toFixed((2322/ 1232).toString().length - 1 ) will round you up the last digit

MrCeRaYA
  • 581
  • 4
  • 6