How do you round to 1 decimal place in Javascript?

How do you round to 1 decimal place in Javascript?

Can you round a number in javascript to 1 character after the decimal point (properly rounded)?
I tried the *10, round, /10 but it leaves two decimals at the end of the int.

Solutions/Answers:

Solution 1:

Math.round( num * 10) / 10 works, here is an example…

var number = 12.3456789;
var rounded = Math.round( number * 10 ) / 10;
// rounded is 12.3

if you want it to have one decimal place, even when that would be a 0, then add…

var fixed = rounded.toFixed(1);
// fixed is always to 1dp
// BUT: returns string!

// to get it back to number format
parseFloat( number.toFixed(2) )
// 12.34
// but that will not retain any trailing zeros

// so, just make sure it is the last step before output,
// and use a number format during calculations!

EDIT: add round with precision function…

Using this principle, for reference, here is a handy little round function that takes precision…

function round(value, precision) {
    var multiplier = Math.pow(10, precision || 0);
    return Math.round(value * multiplier) / multiplier;
}

… usage …

round(12345.6789, 2) // 12345.68
round(12345.6789, 1) // 12345.7

… defaults to round to nearest whole number (precision 0) …

round(12345.6789) // 12346

… and can be used to round to nearest 10 or 100 etc…

round(12345.6789, -1) // 12350
round(12345.6789, -2) // 12300

… and correct handling of negative numbers …

round(-123.45, 1) // -123.4
round(123.45, 1) // 123.5

… and can be combined with toFixed to format consistently as string …

round(456.7, 2).toFixed(2) // "456.70"

Solution 2:

var number = 123.456;

console.log(number.toFixed(1)); // should round to 123.5

Solution 3:

If you use Math.round(5.01) you will get 5 instead of 5.0.

If you use toFixed you run into rounding issues.

If you want the best of both worlds combine the two:

(Math.round(5.01 * 10) / 10).toFixed(1)

You might want to create a function for this:

function roundedToFixed(_float, _digits){
  var rounder = Math.pow(10, _digits);
  return (Math.round(_float * rounder) / rounder).toFixed(_digits);
}

Solution 4:

lodash has a round method:

_.round(4.006);
// => 4

_.round(4.006, 2);
// => 4.01

_.round(4060, -2);
// => 4100

Docs.

Source.

Solution 5:

I vote for toFixed(), but, for the record, here’s another way that uses bit shifting to cast the number to an int. So, it always rounds towards zero (down for positive numbers, up for negatives).

var rounded = ((num * 10) << 0) * 0.1;

But hey, since there are no function calls, it’s wicked fast. 🙂

And here’s one that uses string matching:

var rounded = (num + '').replace(/(^.*?\d+)(\.\d)?.*/, '$1$2');

I don’t recommend using the string variant, just sayin.

Solution 6:

Try with this:

var original=28.453

// 1.- round "original" to two decimals
var result = Math.round (original * 100) / 100  //returns 28.45

// 2.- round "original" to 1 decimal
var result = Math.round (original * 10) / 10  //returns 28.5

// 3.- round 8.111111 to 3 decimals
var result = Math.round (8.111111 * 1000) / 1000  //returns 8.111

less complicated and easier to implement…

with this, you can create a function to do:

function RoundAndFix (n, d) {
    var m = Math.pow (10, d);
    return Math.round (n * m) / m;
}
function RoundAndFix (n, d) {
    var m = Math.pow (10, d);
    return Math.round (n * m) / m;
}
console.log (RoundAndFix(8.111111, 3));

EDIT: see this How to round using ROUND HALF UP. Rounding mode that most of us were taught in grade school