Solved

ASP rounding error on divide, INT function seems incorrect

Posted on 2012-03-12
3
170 Views
Last Modified: 2012-03-13
weightold=351
weightnew=322
weight1b=100

percentgoal=int((weightold-weightnew)/weight1b*100)

This should clearly be exactly 29, but for some reason ASP is spitting out 28.

Thank you!
Screen-Shot-2012-03-12-at-8.00.4.jpg
0
Comment
Question by:bbdesign
3 Comments
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 37712639
<%
weightold=351
weightnew=322
weight1b=100

percentgoal=((weightold-weightnew)/weight1b*100)

response.write percentgoal

%>

Well, I am getting 29
29.JPG
0
 
LVL 15

Accepted Solution

by:
pateljitu earned 500 total points
ID: 37712845
Please use CInt() instead of Int() function, and your code should be changed to:

percentgoal = CInt((weightold-weightnew)/weight1b*100)


Explanation:
CInt() - Returns an expression that has been converted to a Variant of subtype Integer.
Int() - Returns the integer portion of a number.


Here are the articles explaining the functions:
CInt:
http://msdn.microsoft.com/en-us/library/fctcwhw9%28VS.85%29.aspx

Int:
http://msdn.microsoft.com/en-us/library/t4dseb50%28v=vs.85%29.aspx
0
 

Author Comment

by:bbdesign
ID: 37713984
I still don't understand why int("29") would return 28 and cint("29") would return 29. There is nothing to round in this case, because the value I am working with is an exact integer, no decimal points. I have used cint in the past, but then I sometimes run into problems because it only handles integers up to 32767 while int is basically unlimited.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need to rewrite code for checking if a file exists 3 72
Error in query expression 3 54
Help with query 3 36
window close link 7 32
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question