Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This function uses a well known method for rounding to x decimals in javascript
- and makes it more friendly to use. It accepts a value to round and how many
- decimal places we need in the returned value.
- */
- function round(value, decimals){
- var factor = 1;
- // ADDS DECIMAL PLACES TO FACTOR
- for (var i = decimals - 1; i >= 0; i--) {
- factor *= 10;
- };
- // GETS RID OF EVERYTHING NOT NEEDED AND PUTS THE FLOATING POINT BACK IN PLACE
- return Math.round(value * factor) / factor;
- }
- console.log("Rounded to 2 decimal places ->", round(192.34823, 2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement