Warning: session_start(): open(/tmp/sess_330ff045154d1b210cf2492c5eaeb44a, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How to round a number up to the nearest 10 in JavaScript? - LearnShareIT

How to round a number up to the nearest 10 in JavaScript?

Round a Number Up to the Nearest 10 in JavaScript

In this article, you’ll learn how to round a number up to the nearest 10 in JavaScript by using math and the Math class’ static Math.round() function. Let’s read this article now to understand more.

Round a number up to the nearest 10 in JavaScript

Rounding a number up to the nearest 10

Unfortunately, no function explicitly does this. There’s only the Math class’s static Math.round(), Math.floor() or Math.ceil() functions which usually rounds, round down, or round up a number by its first decimal, respectively.
However, our solution lies: If we can move the decimal point to the 1st digit (nearest 10), we can round the number and then move the decimal back to its original position.
You can do this by simply dividing the number by 10, rounding it with the Math.round() function, then multiplying the results by 10 again. For example:

Code:

const number = 123.45;

// Rounding 12.345, then multiply by 10
const roundedNumber = Math.round(number / 10) * 10; 

console.log(roundedNumber);

Output:

120

Rounding a number up to a specified digit or decimal

Taking the logic of the part above, you can make a function to round a number to a specified digit. Simply do the same with the code above but divide and multiply the number by powers of ten (10, 100, 1000, etc.).

You can get powers of ten by using the exponentiation operator (**) or the Math class’ static Math.pow() function. Both generally work the same.

With these things in mind, we have:

const number = 123456789;

// Create a function to round a number to a specified digit
function roundToDigit(number, toDigit) {
	return Math.round(number / (10 ** toDigit)) * (10 ** toDigit);
}

console.log(roundToDigit(number, 2)); // Round a number by the 2nd digit
console.log(roundToDigit(number, 5)); // Round a number by the 5th digit
console.log(roundToDigit(number, 1)); // Round a number by the 1st digit

Output:

123456800
123500000
123456790

However, you might want to round a number by its decimal instead; and to do that, you need to reverse the way you would do it when rounding by digits. I.e., multiplying first, then rounding the number, then dividing it again.

Code:

const number = 123.456789;

// Create a function to round a number to a specified digit
function roundToDecimal(number, toDecimal) {
	return Math.round(number * (10 ** toDecimal)) / (10 ** toDecimal);
}

console.log(roundToDecimal(number, 2)); // Round to 2nd decimal
console.log(roundToDecimal(number, 5)); // Round to 5th decimal
console.log(roundToDecimal(number, 1)); // Round to 1st decimal

Output:

123.46
123.45679
123.5

A thing to note is how the function responds if the toDigit/toDecimal the parameter is not a natural number. One option is throwing an error with the throw keyword. And another is making the parameter a natural number by rounding its absolute value with the Math class’ static Math.abs() function like this:

Code: (Throwing error)

const number = 123456.456789;

function roundToDigit(number, toDigit) {
	// If the digit is a negative number and or is not a natural number, throw an error
	if (toDigit < 0 || Math.round(toDigit) != toDigit) {
		throw "Please enter a natural number";
	}
	return Math.round(number / (10 ** toDigit)) * (10 ** toDigit);
}

function roundToDecimal(number, toDecimal) {
	// If the decimal is a negative number and or is not a natural number, throw an error
	if (toDecimal < 0 || Math.round(toDecimal) != toDecimal) {
		throw "Please enter a natural number";
	}
	return Math.round(number * (10 ** toDecimal)) / (10 ** toDecimal);
}

console.log(roundToDecimal(number, 2)); // Round to 2nd decimal
console.log(roundToDigit(number, 2)); // Round to 2nd digit

// Decimal is a negative number, hence an error will be thrown
console.log(roundToDecimal(number, -4));

// Digit is not a natural number or a positive, hence an error will be thrown
console.log(roundToDigit(number, -1.7));

Output:

123456.46
123500
Uncaught Error Please enter a natural number

Code: (Converting to a Natural number)

const number = 123456.456789;

function roundToDigit(number, toDigit) {
	// Rounds the digit and make it a positive number
	const toDigitFinal = Math.abs(Math.round(toDigit));
	return Math.round(number / (10 ** toDigitFinal)) * (10 ** toDigitFinal);
}

function roundToDecimal(number, toDecimal) {
	// Rounds the decimal and make it a positive number
	const toDecimalFinal = Math.abs(Math.round(toDecimal));
	return Math.round(number * (10 ** toDecimalFinal)) / (10 ** toDecimalFinal);
}

console.log(roundToDecimal(number, 2)); // Round to 2nd decimal
console.log(roundToDecimal(number, -4)); // Round to 4th decimal
console.log(roundToDecimal(number, -1.7)); // Round to 2nd decimal

console.log(roundToDigit(number, 2)); // Round to 2nd digit
console.log(roundToDigit(number, -4)); // Round to 4th digit
console.log(roundToDigit(number, -1.7)); // Round to 2nd digit

Output:

123456.46
123456.4568
123456.46
123500
120000
123500

Summary

To round a number up to the nearest 10 in JavaScript, you need to divide the number by 10, then round the number with the Math class’ static Math.round() function, then multiply it by 10.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *