Javascript round down to 1 decimal

In general, decimal rounding is done by scaling: round(num * p) / p

Naive implementation

Using the following function with halfway numbers, you will get either the upper rounded value as expected, or the lower rounded value sometimes depending on the input.

This inconsistency in rounding may introduce hard to detect bugs in the client code.

function naiveRound(num, decimalPlaces) {
    var p = Math.pow(10, decimalPlaces);
    return Math.round(num * p) / p;
}

console.log( naiveRound(1.245, 2) );  // 1.25 correct (rounded as expected)
console.log( naiveRound(1.255, 2) );  // 1.25 incorrect (should be 1.26)

Better implementations

By converting the number to a string in the exponential notation, positive numbers are rounded as expected. But, be aware that negative numbers round differently than positive numbers.

In fact, it performs what is basically equivalent to "round half up" as the rule, you will see that round(-1.005, 2) evaluates to -1 even though round(1.005, 2) evaluates to 1.01. The lodash _.round method uses this technique.

/**
 * Round half up ('round half towards positive infinity')
 * Uses exponential notation to avoid floating-point issues.
 * Negative numbers round differently than positive numbers.
 */
function round(num, decimalPlaces) {
    num = Math.round(num + "e" + decimalPlaces);
    return Number(num + "e" + -decimalPlaces);
}

// test rounding of half
console.log( round(0.5, 0) );  // 1
console.log( round(-0.5, 0) ); // 0

// testing edge cases
console.log( round(1.005, 2) );   // 1.01
console.log( round(2.175, 2) );   // 2.18
console.log( round(5.015, 2) );   // 5.02

console.log( round(-1.005, 2) );  // -1
console.log( round(-2.175, 2) );  // -2.17
console.log( round(-5.015, 2) );  // -5.01

If you want the usual behavior when rounding negative numbers, you would need to convert negative numbers to positive before calling Math.round(), and then convert them back to negative numbers before returning.

// Round half away from zero
function round(num, decimalPlaces) {
    num = Math.round(Math.abs(num) + "e" + decimalPlaces) * Math.sign(num);
    return Number(num + "e" + -decimalPlaces);
}

There is a different purely mathematical technique to perform round-to-nearest (using "round half away from zero"), in which epsilon correction is applied before calling the rounding function.

Simply, we add the smallest possible float value (= 1.0 ulp; unit in the last place) to the number before rounding. This moves to the next representable value after the number, away from zero.

/**
 * Round half away from zero ('commercial' rounding)
 * Uses correction to offset floating-point inaccuracies.
 * Works symmetrically for positive and negative numbers.
 */
function round(num, decimalPlaces) {
    var p = Math.pow(10, decimalPlaces);
    var e = Number.EPSILON * num * p;
    return Math.round((num * p) + e) / p;
}

// test rounding of half
console.log( round(0.5, 0) );  // 1
console.log( round(-0.5, 0) ); // -1

// testing edge cases
console.log( round(1.005, 2) );  // 1.01
console.log( round(2.175, 2) );  // 2.18
console.log( round(5.015, 2) );  // 5.02

console.log( round(-1.005, 2) ); // -1.01
console.log( round(-2.175, 2) ); // -2.18
console.log( round(-5.015, 2) ); // -5.02

This is needed to offset the implicit round-off error that may occur during encoding of decimal numbers, particularly those having "5" in the last decimal position, like 1.005, 2.675 and 16.235. Actually, 1.005 in decimal system is encoded to 1.0049999999999999 in 64-bit binary float; while, 1234567.005 in decimal system is encoded to 1234567.0049999998882413 in 64-bit binary float.

It is worth noting that the maximum binary round-off error is dependent upon (1) the magnitude of the number and (2) the relative machine epsilon (2^-52).

Round a Number to 1 Decimal Place #

To round a number to 1 Decimal place:

  1. Use the Math.round() function to round the result of multiplying the number by 10.
  2. The Math.round function will round the result to the nearest integer.
  3. Divide the integer by 10 to get the number rounded to 1 decimal place.

Copied!

const num = 5.566; const result = Math.round(num * 10) / 10; console.log(result); // 👉️ 5.6

We used the Math.round function to round the result of multiplying the number by 10 to the nearest integer.

Here are some examples of how the Math.round function works.

Copied!

console.log(Math.round(2.22)); // 👉️ 2 console.log(Math.round(2.5)); // 👉️ 3 console.log(Math.round(2.22 * 10)); // 👉️ 22 console.log(Math.round(2.5 * 10)); // 👉️ 25

The function rounds the number up or down to the nearest integer.

If the number is positive and its fractional part is greater than or equal to 0.5, it gets rounded to the next higher absolute value.

If the number is positive and its fractional portion is less than 0.5, it gets rounded to the lower absolute value.

The last step is to divide the integer by 10 to get the result of rounding the number to 1 decimal place.

Copied!

// 👇️ 2.5 console.log(Math.round(2.456789 * 10) / 10); // 👇️ 4.7 console.log(Math.round(4.689 * 10) / 10);

Alternatively, you can use the Number.toFixed method.

Round a Number to 1 Decimal Place using toFixed() #

Use the toFixed() method to round a number to 1 decimal place, e.g. num.toFixed(1). The toFixed method formats a number to a specified number of decimal places and rounds the number if necessary.

Copied!

const num1 = 5.566; const result1 = num1.toFixed(1); console.log(result1); // 👉️ 5.6 console.log(typeof result1); // 👉️ string // 👇️ if the value is a string // call parseFloat to convert it to a number first const str1 = '5.666'; const result2 = parseFloat(str1).toFixed(1); console.log(result2); // 👉️ 5.7 console.log(typeof result2); // 👉️ string // 👇️ Convert string back to a number const num2 = 5.05; const result3 = Number(num2.toFixed(1)); console.log(result3); // 👉️ 5 console.log(typeof result3); // 👉️ number

In the first example, we used the Number.toFixed method to round a number to 1 decimal place.

The only parameter the method takes is the number of digits that should appear after the decimal point.

The toFixed method returns a string representation of the number.

In our second example, we have a string that is a valid number. We used the parseFloat function to convert the string to a number, because the toFixed method can only be called on numbers.

Copied!

const str1 = '5.666'; const result2 = parseFloat(str1).toFixed(1); console.log(result2); // 👉️ 5.7 console.log(typeof result2); // 👉️ string

In the third example, we used the Number object to convert the string that the toFixed method returned to a number.

Copied!

const num2 = 5.05; const result3 = Number(num2.toFixed(1)); console.log(result3); // 👉️ 5 console.log(typeof result3); // 👉️ number

However, notice that the trailing zero was dropped. This is because JavaScript doesn't keep any insignificant, trailing zeros around.

The number 5.00 is the same as 5, so the trailing zeros get dropped when the value is converted to a number.

Copied!

console.log(5.00 === 5); // 👉️ true

Floating point numbers don't represent all decimals precisely in binary, which can lead to inconsistent results.

Copied!

console.log(0.1 + 0.2 === 0.3); // 👉️ false

The sum of 0.1 and 0.2 is actually equal to 0.30000000000000004 instead of 0.3. This is because the binary floating-point format cannot accurately represent numbers like 0.1 or 0.2.

The code gets rounded to the nearest number, resulting in a rounding error.

Further Reading #

  • Round a Number to 3 Decimal Places in JavaScript
  • Add Leading Zeros to a String in JavaScript

How do you round a number to one decimal place in JavaScript?

To round a number to 1 Decimal place:.
Use the Math. round() function to round the result of multiplying the number by 10 ..
The Math. round function will round the result to the nearest integer..
Divide the integer by 10 to get the number rounded to 1 decimal place..

How do you round down to one decimal place?

When you round to the first decimal place, or to the nearest tenth, the number in the hundredth point place will determine whether you round up or down. If the hundredths digit is a number between 5 and 9, round up to the nearest whole number.

How do you round a number down in JavaScript?

The Math. floor() method rounds a number DOWN to the nearest integer.

How do you round to 2 decimal places in JavaScript?

Use the . toFixed() Method to Round a Number To2 Decimal Places in JavaScript. We apply the . toFixed() method on the number and pass the number of digits after the decimal as the argument.