Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert a value to a safe integer using JavaScript?
An integer is known to be a safe integer if it can be exactly represented as an IEEE-754 double precision number. A safe integer is an integer in the range -(2^53 - 1) to (2^53 - 1). All these numbers are considered safe integers because they allow one-to-one mapping between mathematical integers and their representation in JavaScript.
What is Number.isSafeInteger()?
The Number.isSafeInteger() method determines whether a value is a safe integer. It returns true if the value is a safe integer, otherwise false.
Syntax
Number.isSafeInteger(testValue)
Here testValue is the number to check if it is a safe integer or not.
Example: Check If a Number is a Safe Integer
We can check whether a number is a safe integer using the isSafeInteger() method:
<html>
<body>
<p id="first"></p>
<p id="second"></p>
<p id="third"></p>
<script>
let result1 = Number.isSafeInteger(3);
let result2 = Number.isSafeInteger(3.2);
let result3 = Number.isSafeInteger(Math.pow(2, 53));
document.getElementById("first").innerHTML = "3 is safe: " + result1;
document.getElementById("second").innerHTML = "3.2 is safe: " + result2;
document.getElementById("third").innerHTML = "2^53 is safe: " + result3;
</script>
</body>
</html>
3 is safe: true 3.2 is safe: false 2^53 is safe: false
The number 3 returns true as it's a safe integer, 3.2 returns false as it's not an integer, and 2^53 returns false as it exceeds the safe integer range.
Converting a Number to a Safe Integer
To convert any number to a safe integer, we need to ensure it falls within the safe integer range and is properly rounded:
Algorithm Steps
- Step 1 - Take the input number
-
Step 2 - Clamp it to the maximum safe integer using
Math.min() -
Step 3 - Clamp it to the minimum safe integer using
Math.max() -
Step 4 - Round the result to get an integer using
Math.round()
Example: Convert to Safe Integer
<html>
<body>
<h3>Convert a Number to a Safe Integer</h3>
<button onclick="convertToSafe()">Convert Numbers</button>
<p id="results"></p>
<script>
function convertToSafe() {
// Test different numbers
const testNumbers = [6.7, Math.pow(2, 54), -Math.pow(2, 54), 42.9];
let output = "";
testNumbers.forEach(number => {
const max = Math.min(number, Number.MAX_SAFE_INTEGER);
const min = Math.max(max, Number.MIN_SAFE_INTEGER);
const safeInt = Math.round(min);
output += number + " => " + safeInt + "<br>";
});
document.getElementById("results").innerHTML = output;
}
</script>
</body>
</html>
6.7 => 7 18014398509481984 => 9007199254740991 -18014398509481984 => -9007199254740991 42.9 => 43
Safe Integer Constants
JavaScript provides built-in constants for safe integer boundaries:
console.log("MAX_SAFE_INTEGER:", Number.MAX_SAFE_INTEGER);
console.log("MIN_SAFE_INTEGER:", Number.MIN_SAFE_INTEGER);
console.log("Range:", Number.MIN_SAFE_INTEGER, "to", Number.MAX_SAFE_INTEGER);
MAX_SAFE_INTEGER: 9007199254740991 MIN_SAFE_INTEGER: -9007199254740991 Range: -9007199254740991 to 9007199254740991
Browser Compatibility
Note: The Number.isSafeInteger() method is an ECMAScript 2015 (ES6) feature and is supported in all modern browsers except Internet Explorer.
Conclusion
Use Number.isSafeInteger() to check if a number is within JavaScript's safe integer range. To convert any number to a safe integer, clamp it within the safe bounds and round to the nearest integer.
