- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing

- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM

- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers

- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources

# Rounding off numbers to some nearest power in JavaScript

We are required to write a JavaScript function that takes in a number and returns a number that can be represented as a power of 2 which is nearest to the input number.

For example: If the input number if 145.

Then the output should be 128 because 145 is the nearest such number to 128 which can be represented as 2^n for some whole number value of n.

## Example

The code for this will be −

const num = 145; const nearestPowerOfTwo = num => { // dealing only with non negative numbers if(num < 0){ num *= -1; } let base = 1; while(base < num){ if(num - base < Math.floor(base / 2)){ return base; }; base *= 2; }; return base; }; console.log(nearestPowerOfTwo(num));

## Output

The output in the console −

128

- Related Articles
- Rounding off to nearest thousands :87990
- Give a rough estimate $(by\ rounding\ off\ to\ nearest\ hundreds)$ and also a closer estimate $( by\ rounding\ off\ to nearest\ tens)$:$439+334+4,317$.
- Give rough estimate (by rounding off to nearest hundreds)439 $+$ 334 $+$ 4317
- How to round off numbers In nearest $100$.
- How to round off the numbers nearest tens?
- Estimate each of the following by rounding off each number to nearest hundreds:\( 874+478 \)
- Rounding and truncating numbers in JavaScript.
- Estimate the value of 26895 $+$ 345 by rounding off each number to it's nearest 100.
- Give a rough estimate (by rounding off to nearest hundreds) and also a closer estimate (by rounding off to nearest tens) :(a) $439 + 334 + 4,317$(b) $1,08,734 – 47,599$(c) $8325 – 491$(d) $4,89,348 – 48,365$Make four more such examples.
- What is mean by rounding off?
- Estimate the following products by rounding off each of its factors nearest to its greatest place:(i) \( 578 \times 161 \)
- Nearest power 2 of a number - JavaScript
- Round number down to nearest power of 10 JavaScript
- Round off 1120 to nearest hundred.
- How to round off to nearest thousands?

Advertisements