Click to See Complete Forum and Search --> : rounding to nearest number in a java calculator


stephenrulz02
12-05-2003, 03:52 AM
I have tried a search, but none of the results solved my problem :(


I wrote this code for no particular reason, just to have a calculator to find 90% of the value entered.

I have attached a txt file with the code and a working calculator can be found at:

http://www.angelfire.com/un/neoguide/powerpets/javacalc.html

To get it to work, you:
1. Enter a Value
2. Click the button labeled 'PT'
3. and then 'Click to Calculate' to get the answer


but I realized I would like this calculator to do more, to be able to round the numbers up or down.

For example:
If after clicking 'Click to Calculate' the number is found is 2.44, I would like it to display 2 and if it is 2.45 it should display a 3

I think I have found the funtion I need to write some if statements in, but being new to Java I don't really know how to get it to do what I want.

function finish()
{
document.Screen.secret.value = eval(document.Screen.secret.value)
FINAL = document.Screen.secret.value
document.Screen.display.value = FINAL
}

That function is the one called when 'Click to Calculate' is pressed.


Is there anyway this code can be modifyed to allow this rounding off?

Thanks everyone for your time :)

stephenrulz02
12-05-2003, 03:54 AM
I don't see where the attachment was attached... so it is on this one i think...

Sorry :(

Gollum
12-05-2003, 03:57 AM
You could possibly use Math.round()...

var n = 2.44;
var r = Math.round(n);
// r == 2

n = 2.51;
r = Math.round(n);
// r == 3

The only worry is that Math.round() would round 2.45 down to 2, not up to 3.

stephenrulz02
12-05-2003, 04:01 AM
Originally posted by Gollum
You could possibly use Math.round()...

var n = 2.44;
var r = Math.round(n);
// r == 2

n = 2.51;
r = Math.round(n);
// r == 3

The only worry is that Math.round() would round 2.45 down to 2, not up to 3.

that would be fine, i'll add it and check it out!

Thanks a lot!!!
:)

stephenrulz02
12-05-2003, 04:03 AM
Originally posted by Gollum
You could possibly use Math.round()...

var n = 2.44;
var r = Math.round(n);
// r == 2

n = 2.51;
r = Math.round(n);
// r == 3

The only worry is that Math.round() would round 2.45 down to 2, not up to 3.



any way you would know if that is exactly what could be put in and where to put it? Or do I need other code to go with it?

Thanks again :)

Gollum
12-05-2003, 04:07 AM
No, Math.round() is a function that comes built in to JavaScript. Just use it whereever you want to round a number to the nearest integer.

stephenrulz02
12-05-2003, 04:12 AM
Originally posted by Gollum
No, Math.round() is a function that comes built in to JavaScript. Just use it whereever you want to round a number to the nearest integer.


ok, thanks again!!


:D I didn't even expect an answer that fast... this place is great! :D

:)

Pittimann
12-05-2003, 04:22 AM
Hi!

I'm wondering a bit, why you want to round 2.44 down to 2 and 2.45 upto 3.

Did I get you right - any x.44 round down to x and any x.45 round up to x + 1? If so:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
function calc(){
n = document.myForm.Number.value;
var r = Math.round(n);
if (r+0.44<n)r= r+1;
alert(r);
}
</script>
</head>
<body>
<form name="myForm">
<input type="text" name="Number"><br>
<input type="button" onclick="calc()" value="calculate">
</body>
</html>

Cheers - Pit

stephenrulz02
12-05-2003, 04:29 AM
Originally posted by Pittimann
Hi!

I'm wondering a bit, why you want to round 2.44 down to 2 and 2.45 upto 3.

Did I get you right - any x.44 round down to x and any x.45 round up to x + 1? If so:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
function calc(){
n = document.myForm.Number.value;
var r = Math.round(n);
if (r+0.44<n)r= r+1;
alert(r);
}
</script>
</head>
<body>
<form name="myForm">
<input type="text" name="Number"><br>
<input type="button" onclick="calc()" value="calculate">
</body>
</html>

Cheers - Pit

I think it was just a case of me not knowing exactly what I wanted... that happens a lot ;)

After playing around with the finish() function, I got it work the way I had in mind but didn't do so well expressing to you all:


function finish()
{
document.Screen.secret.value = eval(document.Screen.secret.value)
FINAL = Math.round(document.Screen.secret.value);
document.Screen.display.value = FINAL
}

But thanks for your help :)