Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Rounding off numbers using Javascript

    Hello, looking for some help if possible as whatever solution I try isnt working, and this is probably something so simple but its driving me up the wall now!

    I have the following script:

    Code:
    calculate = function()
    {
        var textInput = document.getElementById('textInput').value;
        var textInput2 = document.getElementById('textInput2').value;
        document.getElementById('Total1').value = parseInt(textInput)/parseInt(textInput2)/12;
        document.getElementById('Total2').value = parseInt(textInput)/100*103.83;
       }
       
    function show_value2(x)
    {
     document.getElementById("slider_value2").innerHTML=x;
    }
    
    function show_value3(x)
    {
     document.getElementById("slider_value3").innerHTML=x;
    }
    This basically takes 2 numbers (divides one by the other, and then adds 3.83% onto the final output). Its for a home owners loan calculator. See here: Home | Riverlight Finance

    I need to round these numbers up though (so this: 9723.538461538461 becomes this: 9723)

    Thank you

  2. #2
    New to the CF scene
    Join Date
    Apr 2017
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    The code I try:
    Code:
      
    
    <script type="text/javascript">
    
    // 9723.538461538461 becomes this: 9723
    
    var n = 9723.538461538461;
    
    var s= String(n);
    alert(s[0]); // 9
    
    var A = [ ];
    for(var i=0; s[i] != "."; i++) {
    A[i] = s[i];
    }
    alert(A);//9,7,2,3
    
    var t = A.join("");
    // alert( t ); // 9723
    // alert(typeof(t));// string
    t = Number(t); 
    // alert(typeof(t)); // number
    // alert(t +4); // 9727
    
    </script>

  3. #3
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    2,310
    Thanks
    2
    Thanked 334 Times in 324 Posts
    What you are looking for is Math.round. Parseint is a truncate -- equivalent to Math.floor

    Also do you REALLY mean to truncate the user input?

    but yeah, Math.round(1.56) gives you 2.

    It's important to know all three: Math.round, Math.floor, and Math.ceil (ceiling)

    Math.round(1.001) == 1
    Math.round(1.5) == 2
    Math.round(1.999) == 2

    Math.floor(1.001) == 1
    Math.floor(1.5) == 1
    Math.floor(1.999) == 1

    Math.ceil(1.001) == 2
    Math.ceil(1.5) == 2
    Math.ceil(1.999) == 2
    I would rather have questions that can't be answered, than answers that can't be questioned.
    http://www.cutcodedown.com

  4. #4
    New to the CF scene
    Join Date
    Apr 2017
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Code:
      
    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    
    // 9723.538461538461 becomes this: 9723
    
    // "up","down","nearest"
    Math.roundToInteger = function(num,txt){
    var s = String(num);
    
    var len = s.indexOf(".");
    var b = len; // basamak
    // alert("len = "+len);
    var num=0;
    for(var i=0; s[i] != "."; i++) {
    b = b -1;
    num += s[i]* Math.pow(10,b)  ;
    // alert("num = "+num);
    // alert("b= "+b);
    }
    var dec = s[i+1]; 
    //alert("i = "+i); // 4
    if( txt == "nearest" && dec >= 5 ){ txt = "up";}
    if( txt == "nearest" && dec < 5 ){ txt = "down";}
    if( txt == "up"){ return (num + 1);}
    if( txt == "down"){ return num;}
    
    }
    var u = Math.roundToInteger(9723.538461538461, "up");
    alert("u= "+ u); // 9724
    var d = Math.roundToInteger(9723.538461538461, "down");
    alert( "d= "+ d ); // 6723
    var n = Math.roundToInteger(9723.538461538461, "nearest");
    alert( "n= "+ n );  // 9724
    
    alert(Math.ceil(9723.538461538461)); // 9724
    alert(Math.floor(9723.538461538461)); // 9723
    alert(Math.round(9723.538461538461)); //9724
    
    </script>
    </head>
    <body>
    
    </body>
    </html>



    try ithttps://www.w3schools.com/code/tryit...e=FHZY9RYHD6Y1

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post
    Code:
    function decimalAdjust(type, value, exp) {
    
        if (typeof exp === 'undefined' || +exp === 0) {
          return Math[type](value);
        }
        value = +value;
        exp = +exp;
    
        if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
          return NaN;
        }
       
        if (value < 0) {
          return -decimalAdjust(type, -value, exp);
        }
        // Shift
        value = value.toString().split('e');
        value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
      
        value = value.toString().split('e');
        return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
      }
    
      if (!Math.round10) {
        Math.round10 = function(value, exp) {
          return decimalAdjust('round', value, exp);
        };
      }
    
      if (!Math.floor10) {
        Math.floor10 = function(value, exp) {
          return decimalAdjust('floor', value, exp);
        };
      }
    
      if (!Math.ceil10) {
        Math.ceil10 = function(value, exp) {
          return decimalAdjust('ceil', value, exp);
        };
      }
    })();
    Last edited by vinyl-junkie; 08-23-2017 at 02:33 PM. Reason: added code tags


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •