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
 
Hi vbkris,

Can you use Math.ceil():

Code:
function Round(TheValue,ThePoints)
{
    TheValue=Math.ceil(TheValue)
    document.getElementById("RoundDiv").innerHTML=TheValue
}

Thanks,
 
nope,
ceil is a problem, if its 198.2 it becomes 199...



Known is handfull, Unknown is worldfull
 
>i just want to round it off to the nearest integer...
Is it not 198.45 nearer to 198 than 199?

Whatever meaning, "round upward" (figuratively) can always be done like this for positive number.
[tt] TheValue=(Math.floor(TheValue)==TheValue)?TheValue:Math.floor(TheValue)+1;[/tt]
The question remains incomplete usually on the behaviour of negative number.
 
>>Is it not 198.45

mathematically speaking no, 198.45 is first rounded off to 198.5 then finally to 199...

Known is handfull, Unknown is worldfull
 
got it:
Code:
<script>
function Round(TheValue,ThePoints)
{
	TheReminder=TheValue.replace(/[\d]+\.([\d]+)$/,"$1")
	TheLen=TheReminder.length-1
	while(TheLen>=0)
	{
		TheValue=Math.round(TheValue*Math.pow(10,TheLen))/Math.pow(10,TheLen)
		TheLen--
	}
	document.getElementById("RoundDiv").innerHTML=TheValue
}
</script>

Known is handfull, Unknown is worldfull
 
>got it:
Is that what you call got it? Well... I suppose everyone gets one's own idea.
 
yup,

in mathematical terms when u average out points u have to start from the right most extreme.

e.g:
3.44445

becomes
3.4445
3.445
3.45
3.5
4

thats the correct rounding procedure (especially when money payments are involved)...




Known is handfull, Unknown is worldfull
 
Does this meet specs?..

Code:
function Round(TheValue,ThePoints)
{
    TheValue=Math.round(parseFloat(TheValue).toFixed(1));
    document.getElementById("RoundDiv").innerHTML=TheValue
}

Thanks,
 
>thats the correct rounding procedure (especially when money payments are involved)...
Checkout this candide discussion.
especially Banker's rounding. Rounding is not a pure arithmetic convention. Statistical bias is a consideration as well in practice. "Correct" procedure? Maybe your accountants/clients have their own idea. The "got" procedure has an obvious bias.
 
>>The "got" procedure has an obvious bias.

sure, but like the link said arithmatic does the plain rounding.

Banker's rounding may be biased but if u look at from a user's point of view then there is a problem, 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!!! now as a buyer what would u think of the site???

>>Lrnmore
will get back to u...

Known is handfull, Unknown is worldfull
 
vbkris,

My gaffe, the toFixed() is no better.
I thought it "extended" the round, but it also needs "staging".

Code:
function Round(TheValue,ThePoints)
{
    TheReminder=TheValue.replace(/[\d]+\.([\d]+)$/,"$1")
    TheLen=TheReminder.length-1
    while(TheLen>=0)
    {
        TheValue=parseFloat(TheValue).toFixed(TheLen)
		TheLen--
    }
    document.getElementById("RoundDiv").innerHTML=TheValue
}

Thanks,
 
mine
>The "got" procedure has an obvious bias.
Your saying is that the continuum which is liable to be regrouped to 199 (besides this is what we call integer, no any of 198.445 or 198.4445 etc.) is 198.45 to 199.45, period. That's the bias. (198.4444444... ad infinum is 198.45)

The rest the readers will judge and take useful elements. That's all what I want to make clear. The op can fabricate all the excuse and good reasons. It is up to the op.
 
I don't know what kind of arithmetic the OP is using, but I've never heard of having to round each decimal position one at a time. There's no point, and it gives what I would consider incorrect results. I don't believe that 198.45 should ever "round" to 199.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
>in mathematical terms when u average out points u have to start from the right most extreme.

Really? Ok, let me know when you get done rounding pi.

Adam
 
>in mathematical terms when u average out points u have to start from the right most extreme.

And what does "average" have to do with rounding?

Rounding is as much (or more) about statistics as it is about math. I once did a graph that showed accumulated rounding errors for several different rounding methods. The only one that did not show a constantly increasing rounding error is the technique of rounding DOWN on 0-4, rounding UP on 6-9, and rounding the preceeding digit to an EVEN (or ODD) number when the rounding digit is 5.

That is because when the rounding part is exactly halfway between you will be rounding up and down an equal number of times (statistically speaking, depending on the actual numbers, your results may vary).

Note that when I mention rounding down on 0-4, etc. that doesn't mean you have to do it for each digit - that would actually throw off the results. It means 00000 to 49999, 50000, and 50001 to 99999, for however many positions you need to round off. The important thing is that 50000... gets rounded up half the time and down half the time.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
>>I agree. 198.45 rounded to the nearest integer is 198. How anyone can say it is anything else, and not be ridiculed is beyond me! It's simple maths!

since i seem to be inviting curt replies i need to now go back to basics.

when given sums like this (Round off to 2 places)

1. 12.953
2. 13.495
3. 14.945

the reply that we give is
1. 12.95
2. 13.50
3. 14.95

thats precisely what i have tried to implement in the code, thats the start and end ot it. there is no big level thinking beyond that.


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

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top