Rounding a number can't be this hard

# Thread: Rounding a number can't be this hard

1. Junior Member
Join Date
Dec 1969
Posts
6

## Rounding a number can't be this hard

I have a .asp file that retrieves some numbers from a database and divides them. Here is the code:<BR><BR> DG(I) = cint(Request("DG" & I))<BR> DR(I) = cint(Request("DR" & I))<BR> if (Maps(I) = "[None]") then<BR> DRO(I) = 0<BR> elseif (DR(I) &#062; 0) then<BR> DRO(I) = Round(DG(I)/DR(I), 4)<BR> else<BR> DRO(I) = 100<BR> end if<BR><BR>Most of the values will fall into the elseif section where they get rounded. If I response.write the value of DRO(I) after the end if, it will show up nice and rounded to 4 decimal places, no problem.<BR><BR>Later on the page, I have this section:<BR><BR> if DRO(W) &#062;= FH then<BR> FH = FH + 1<BR> response.write DRO(W)<BR> end if<BR><BR>At this point in the file, by some totally inexplicable reason that&#039;s driving me insane, the value in DRO(W) is reverted back to a value with more than 4 decimal places!<BR><BR>Consider:<BR><BR>o There is no code whatsoever in between the two sections that is modifying DRO(x).<BR><BR>o I tried putting a response.write on the value in DRO(W) just before the "if DRO(W) &#062;= FH then" line and it prints with 4 decimal places, but if I put another identical response.write inside the if statement or after the "end if", it goes back to &#062;4 decimal places! It&#039;s as if the IF statement is "unrounding" the value.<BR><BR>o If I modify the if line to be "if round(dro(w), 4) &#062;= FH then", it will "re-round" it back to 4 decimal places, and the response.write dro(w) after the "if round(dro(w), 4) &#062;= FH then" line shows up with 4 decimal places for the rest of the file.<BR><BR>o The most freakish thing of all... In the first section where the initial value of DRO(I) is assigned, I tried this:<BR><BR> elseif (DR(I) &#062; 0) then<BR> blah = Round(DG(I)/DR(I), 4)<BR> DRO(I) = blah<BR> else<BR><BR>In doing this, DRO(I) will *never* hold the unrounded value ever, but by some supernatural force, it STILL showed up with &#062;4 decimal places later on in the DRO(W) section! And if I response.write DRO(I) above after the "DRO(I) = blah" line it shows up rounded off! I&#039;ve been programming in basic for many years and I&#039;ve never seen behavior like this.<BR><BR>How can this possibly happen? How can these numbers "unround" themselves? <BR><BR>

2. Senior Member
Join Date
Dec 1969
Location
Indianapolis, IN
Posts
1,755

## Pretty strange

I could see this not working:<BR>blah = Round(DG(I)/DR(I), 4)<BR>DRO(I) = blah<BR>because DRO(I) could be a variant data type that just behaves strange.<BR><BR>Anyway, what I would do in your situation is change this part:<BR>if DRO(W) &#062;= FH then<BR>FH = FH + 1<BR>response.write DRO(W)<BR>end if<BR><BR>to:<BR>blah = DRO(W)<BR>if blah &#062;= FH then<BR>FH = FH + 1<BR>response.write DRO(W)<BR>end if<BR><BR>If it really is the conditional that throws it off, don&#039;t let the conditional use DRO(W).

3. Senior Member
Join Date
Dec 1969
Posts
3,195

## I would debug the code

more. Obviously you are only showing us part of the code for that first part so i&#039;m assuming its part of a for..next loop. I know you response.writed individual values, but for the sake of debugging how about writing out all the values of the array in one shot.<BR><BR>So after your "Next" statement write out all the values of the array:<BR><BR>for j = 0 to ubound(DRO)<BR> response.write "Value " & cstr(j+1) & " = " & DRO(j)<BR>next<BR><BR>then later on in your code before that other block of code put<BR>the same check<BR><BR>for j = 0 to ubound(DRO)<BR> response.write "Value " & cstr(j+1) & " = " & DRO(j)<BR>next<BR><BR>Is it the same? Is there relevant code you are not showing us? Are you doing "anything" to DRO values?<BR>

4. Senior Member
Join Date
Dec 1969
Posts
3,195

## Is this code inside

subs/functions? Do you have similar named variables? Is this a case of changing the "wrong" variable value?<BR><BR>Give us more info. ; )

5. Junior Member
Join Date
Dec 1969
Posts
6

## RE: Pretty strange

YES, MedevalDude! This worked! Thank you SO much!<BR><BR>Now my question is why was it happening in the first place? The thing that really drove me crazy is how the unrounded values could get into DRO(W) when in the first section where they were initially assigned I used a separate variable (blah) and then had "DRO(I) = blah". There was no way the unrounded values could get into DRO(I), but somehow they were. <BR><BR>Also, thank you to the others that responded. I could post the whole thing, but it&#039;s a very long file and I&#039;m not sure what kind of limits they have on that. But there are no functions or subs at all in the file, and the area between where the values were assigned to DRO(I) and compared later (DRO(W)) there were no modifications whatsoever. I retried running the script about 4 times to make sure what was happening was happening, I rebooted the server & tried it another 2 times, and I poured over the code (& did Find) to ensure that nothing was modifying the value.<BR><BR>The mere fact that a response.write immediately before the "if DRO(W) &#062;= FH then" line was rounded and another one immediately after the "if DRO(W) &#062;= FH then" line or after the "end if" was unrounded is just the strangest thing I&#039;ve ever seen. I&#039;d like someone to explain that one. ;)<BR> <BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•