1

I am trying to create a function that can take a number and the number of decimal places and round the number to the exact decimal places that are going to be given. I am using parseInt(prompt()) in order to gave the number and the number of decimal places.

For example,

round(3.141519, 2) -> 3.14
round(5986.32456, 4) -> 5986.3246

Can someone help me with this?

martianwars
  • 6,380
  • 5
  • 35
  • 44
Leo
  • 23
  • 3
  • Google: [site:stackoverflow.com javascript round to decimal](https://www.google.com/search?q=site%3Astackoverflow.com+javascript+round+to+decimal) –  Dec 20 '16 at 19:24
  • Can't you use Math.round? https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Math/round – Hosar Dec 20 '16 at 19:25
  • Possible duplicate of [JavaScript math, round to two decimal places](http://stackoverflow.com/questions/15762768/javascript-math-round-to-two-decimal-places) – Heretic Monkey Dec 20 '16 at 19:25
  • 1
    Possible duplicate of [Formatting a number with exactly two decimals in JavaScript](http://stackoverflow.com/questions/1726630/formatting-a-number-with-exactly-two-decimals-in-javascript) – Ninja Dec 20 '16 at 19:26

2 Answers2

2

You can use toFixed

check the following

 console.log(3.141519.toFixed(2))
 console.log(5986.32456.toFixed(4))
Geeky
  • 7,420
  • 2
  • 24
  • 50
1

Here is a short function that will allow you to specify the precision and returns a number:

function round(number, places) {
   number = parseFloat(number, 10);
   var e  = parseInt(places || 2, 10);
   var m = Math.pow(10, e);
   return Math.floor(number * m) / m;
}

Or a slightly shorter ES6 function:

const round = (number, places=2) => {
   const m = Math.pow(10, places);
   return Math.floor(number * m) / m;
}
Rob M.
  • 35,491
  • 6
  • 51
  • 50
  • I used the first solution, and when i run the function it gives me NaN. – Leo Dec 20 '16 at 21:55
  • Shouldn't it print out the rounded number with as many decimal places that i want? – Leo Dec 20 '16 at 21:57
  • @Leo Are you not passing it numbers? `round(3.1456, 3)` works for me... – Rob M. Dec 20 '16 at 21:59
  • I updated my first solution to use `parseFloat` and `parseInt` in case you pass it strings – Rob M. Dec 20 '16 at 22:03
  • I guess i am doing something wrong with my solution.. I can't figure out what. – Leo Dec 20 '16 at 22:05
  • function round(number, places) { var number = parseInt(prompt("Please enter number:")); var places = parseInt(prompt("Please enter number of decimal places:")); var e = places || 2; var m = Math.pow(10, e); return Math.floor(number * m) / m; } document.write(round()); Where is my mistake? Can you tell me? – Leo Dec 20 '16 at 22:11
  • Yeah, you are using `parseInt` on the "Please enter a number", that needs to be `parseFloat`. You could also remove the `number` and `places` arguments from the function declaration since you are getting that data using `prompt` – Rob M. Dec 20 '16 at 22:17
  • Thanks! You ROCK! :D – Leo Dec 20 '16 at 22:18
  • Glad to help, Leo! :D – Rob M. Dec 20 '16 at 22:18