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