Solved

ASP rounding error on divide, INT function seems incorrect

Posted on 2012-03-12
3
161 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now