Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rounding problem!!!

Status
Not open for further replies.

vbkris

Programmer
Jan 20, 2003
5,994
IN
This is the code:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<script>
function Round(TheValue,ThePoints)
{
	TheValue=Math.round(TheValue)
	document.getElementById("RoundDiv").innerHTML=TheValue
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<input type="text" name="t1" onChange="Round(this.value)">
<div id="RoundDiv"></div>
</body>
</html>

try entering 198.45

it gives 198, is there a way to make it show 199.

in my example i have given just 2 decimal places actually it can also be like this:
198.445
198.4565455

i just want to round it off to the nearest integer...

Known is handfull, Unknown is worldfull
 
oh no, that was an example, finally i WANT an integer, so i simply extended the process...

Known is handfull, Unknown is worldfull
 
so i am correct???

Known is handfull, Unknown is worldfull
 
> since i seem to be inviting curt replies i need to now go back to basics

Good idea, you should research the basics. If you run across the technique you describe, please post a link to it. So far every single example on the web I've seen on how to round numbers says you only need to look at the digit to the right of the position you want to round to.

Example:



Adam
 
hmm,
seems u r correct, will try to get some opinion on this...

Known is handfull, Unknown is worldfull
 
>>Really? Ok, let me know when you get done rounding pi.

thats a mathematiacal impossibility, just because Pi cannot be rounded off doesnt mean we drop the rounding concept itself. we try to do what can be done...

No, it is NOT a "mathematical impossibility"! You can easily round pi to however many decimal places you need. All of the following are pi, rounded:

3
3.1
3.14
3.142
3.1416
3.14159

If you couldn't round the value of pi to some finite number, a major portion of geometry, trigonometry, engineering and a host of other disciplines would be impossible.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
I think vbkris is a lil confused on his math. From everything I've learned in school, I've never EVER heard anyone say you round from the far right. The one thing I paid attention to in school was math class. When you round 399.5 it becomes 400 but if u try to round 399.499... it becomes 399. Use any calculator or any other device. I have yet to see any situation were you should round the other way and I don't understand why anyone would.
 
>>I think vbkris is a lil confused on his math

more than u think buddy, u dont know what kind of embarassment it can be to be proven wrong where u were 100% sure that u were right! now what can one say???

Known is handfull, Unknown is worldfull
 
On the contrary, I'd bet most of us have had the same kind of embarassment. Or would I embarass myself and lose that bet?

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
i didn't mean to be rude. I just reread what I put and it sounded bad but I was reading it and you said something about

>> one day i buy for 3.5 and the site charges me 4 dollars, another day i buy for 2.5 it charges me 2!!!

I just don't understand the reasoning behind that
 
that was the banker's rounding concept...

Known is handfull, Unknown is worldfull
 
wow ummm the concept is messed up then because that is like saying 3.5 could be 4 or 3... unless there is something I'm missing
 
Technically speaking, when it comes to rounding 3.5 COULD be 4 or 3. It is EXACTLY halfway between 4 and 3, so which way do you round? Statisically, you should round up half the time and round down half the time. Since that's generally impractical, you have to decide for yourself which way you want to round. If you're rounding a lot of different numbers, you can round .5 so that the preceeding digit is always odd or always even and that will take care of the problem.

Example of "rounding even":

0.5 -> 0 rounding error: -.5
1.5 -> 2 +.5
2.5 -> 2 -.5
3.5 -> 4 +.5
4.5 -> 4 -.5
5.5 -> 6 +.5
6.5 -> 6 -.5
7.5 -> 8 +.5
8.5 -> 8 -.5
9.5 -> 10 +.5

Total rounding error: 0


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
...this is too technical for me. Just never round and then that way everyone can be happy. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top