Skip to content
Related Articles

Related Articles

How to convert a value to a safe integer using JavaScript ?
  • Last Updated : 18 Apr, 2021

In this article, we will learn How to convert a value to a safe integer using JavaScript. We have given a number, and we have to convert it into a safe integer using JavaScript. Safe Integers are integers between -(253 – 1) and (253 – 1).

Approach: First, we take an input using the prompt in JavaScript. And store that input in a variable called input. Now we make two variables, one called mini that contains the minimum of input and the MAX_SAFE_INTEGER[253-1], and the other is maxi that contains the maximum of mini and MIN_SAFE_INTEGER[-(253-1)]. Now we make another variable that contains our answer called safeInt that contains the round value of the maxi variable.  And this is the safe integer of the given value. Below is the all syntax of the above-used JavaScript functions:

Syntax:

const input = prompt('Please enter umsafe integer:');
var mini = Math.min(input,Number.MAX_SAFE_INTEGER);
var maxi = Math.max(mini,Number.MIN_SAFE_INTEGER);
const safeInt = Math.round(maxi);

Example:

HTML




<!DOCTYPE html>
<html>
<head>
    <style>
        .gfg1{
            font-size: 30px;
            color: green;
        }
        #gfg2{
            font-size: 30px;
            color: green;    
        }
        div{
            margin-left: 30%;
        }
        button{
            font-size: 20px;
        }
    </style>
</head>
<body>
    <div>
        <p class="gfg1">GeeksforGeeks</p>
  
        <button onclick="fun()">click me</button>
        <p id="gfg2"></p>
  
    </div>
    <script>
        function fun(){
            const input = prompt('Please enter umsafe integer:');
            var mini = Math.min(input,Number.MAX_SAFE_INTEGER);
            var maxi = Math.max(mini,Number.MIN_SAFE_INTEGER);
            const safeInt = Math.round(maxi);
            document.getElementById("gfg2").innerHTML = input + " => " + safeInt;
        }
    </script>
</body>
</html>

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :