Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Rounding off numbers using Javascript

1. ## 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. The code I try:
Code:

<script type="text/javascript">

// 9723.538461538461 becomes this: 9723

var n = 9723.538461538461;

var s= String(n);

var A = [ ];
for(var i=0; s[i] != "."; i++) {
A[i] = s[i];
}

var t = A.join("");
// alert( t ); // 9723
t = Number(t);

</script>

3. 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

4. Code:

<!DOCTYPE html>
<html>
<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
var num=0;
for(var i=0; s[i] != "."; i++) {
b = b -1;
num += s[i]* Math.pow(10,b)  ;
}
var dec = s[i+1];
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");
var d = Math.roundToInteger(9723.538461538461, "down");
alert( "d= "+ d ); // 6723
var n = Math.roundToInteger(9723.538461538461, "nearest");
alert( "n= "+ n );  // 9724

</script>
<body>

</body>
</html>

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

5. Code:

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) {
}
// 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) {
};
}

if (!Math.floor10) {
Math.floor10 = function(value, exp) {
};
}

if (!Math.ceil10) {
Math.ceil10 = function(value, exp) {
};
}
})();