Open In App

Gaussian/banker’s rounding in JavaScript

Improve
Improve
Like Article
Like
Save
Share
Report

The task is to implement the Gaussian/banker’s rounding with the help of JavaScript. Here, few approaches are discussed.

Approach 1: Get the d value(which tells the digits to which we need to round off). Calculate the m(pow(10, d)). Then shift the decimal after the digits we want to round off(if d=2, shift it to 2 places right of n). Then get the floor value of n(which is i) and calculate the difference between the original value and floor value of n. Check the condition whether the value which is going to return is i or i+1, if the condition matches else return i(store the value in r). If d=0 return r else return r/m.

  • Example: This example implements the above approach.




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            Gaussian/banker's rounding in JavaScript
        </title>
    </head>
      
    <body style="text-align:center;">
        <h1 style="color:green;">   
                GeeksforGeeks   
            </h1>
        <p id="GFG_UP" 
           style="font-size: 20px; 
                  font-weight: bold">
        </p>
        Type Here:
        <input id="inp" />
        <br>
        <br>
        <button id="button" onclick="GFG_Fun();">
            click here
        </button>
        <p id="GFG_DOWN"
           style="font-size: 26px;
                  font-weight: bold;
                  color: green;">
        </p>
        <script>
            var up = document.getElementById('GFG_UP');
            var down = document.getElementById('GFG_DOWN');
            up.innerHTML =
    "Type number and click on the button to round the number"
            +" in the Gaussian/banker's format.";
      
            function roundIt(n, d = 0) {
                var m = Math.pow(10, d);
                var n = +(d ? n * m : n).toFixed(8);
                var i = Math.floor(n),
                    diff = n - i; // getting the difference
                var e = 1e-8; // Rounding errors in var(diff)
                // Checking if the difference is less than or
                // greater than, based on that adding the 1 to it.
                var r = (diff > 0.5 - e && diff < 0.5 + e) ?
                    ((i % 2 == 0) ? i : i + 1) : Math.round(n);
                return d ? r / m : r; // if d != 0 then returning r/m else r
            }
      
            function GFG_Fun() {
                var inp = Number(document.getElementById('inp').value);
                down.innerHTML = roundIt(inp);
            }
        </script>
    </body>
      
    </html>

    
    

  • Output:

Approach 2: Get the d value(which tells the digits to which we need to round off). Shift the decimal to right of n according to d. Then get the round value of n(which is r). If the absoluteValue(n) % 1 == 0.5 and if the r is even return r else r-1 and if absoluteValue(n) % 1 != 0.5 then return r.

  • Example: This example implements the above approach.




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            Gaussian/banker's rounding in JavaScript
        </title>
    </head>
      
    <body style="text-align:center;">
        <h1 style="color:green;">   
                GeeksforGeeks   
            </h1>
        <p id="GFG_UP"
           style="font-size: 20px;
                  font-weight: bold">
        </p>
        Type Here:
        <input id="inp" />
        <br>
        <br>
        <button id="button"
                onclick="GFG_Fun();">
            click here
        </button>
        <p id="GFG_DOWN" 
           style="font-size: 26px;
                  font-weight: bold;
                  color: green;">
        </p>
        <script>
            var up = document.getElementById('GFG_UP');
            var down = document.getElementById('GFG_DOWN');
            up.innerHTML =
                "Type number and click on the button to round " +
                "the number in the Gaussian/banker's format.";
      
            function roundIt(n, d = 0) {
                var n = n * Math.pow(10, d);
                var r = Math.round(n);
                var val = 
            Math.abs(n) % 1 === 0.5 ? (r % 2 === 0 ? r : r - 1) : r;
                return val / Math.pow(10, d);
            }
      
            function GFG_Fun() {
                var inp = Number(document.getElementById('inp').value);
                down.innerHTML = roundIt(inp);
            }
        </script>
    </body>
      
    </html>

    
    

  • Output:


Last Updated : 14 Feb, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads