Solved

ASP rounding error on divide, INT function seems incorrect

Posted on 2012-03-12
3
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 29

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

632 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