Jump to content
  • Advertisement
Sign in to follow this  
MaDCaTz

Need help with Visual Basic number Rounding

This topic is 5472 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

in a vb game im making, i need to have a code which will divide the funds by the cost of a product to work out how many you can buy. maxfood = Val(txtfunds.Text) / Val(txtfood.Text) the code above will do that for me but it usally gives back a value of something like 47.4643161. in which i want it to only round down to make it 47. i have found this code: value = CSng(maxfood) rounded = CInt(value) truncated = Int(value) maxfood = Format$(truncated) which will do that for me, but for some reason, when the value it should round goes into 100,000's it will give an overflow error and put the value as "0" What code shall i use? Is there a way which will take away any decimals as it will do the same... if you catch what i mean.

Share this post


Link to post
Share on other sites
Advertisement
use long instead of int.

int is 16 bit (goes up to 2^16 = 65536),
while long is 32 bit (2^32 = 4294967296).

That should solve your problem.
(long = no decimals, so it will interchange fine) [wink]

Share this post


Link to post
Share on other sites
That would be CLng() and Lng() instead of CInt() and Int(). Dimming your variables to explicit types is also a good idea (ie, Dim value as Long).

shmoove

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!