Need help with Visual Basic number Rounding

Started by
2 comments, last by shmoove 19 years, 7 months ago
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.
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]
Yeh, great thanks... how do i use that? :)
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

This topic is closed to new replies.

Advertisement