Visual Basic

Currently reading:
Visual Basic

Joined
Apr 21, 2003
Messages
22,169
Points
3,110
Location
Nottm
I ask on here because 1) I am too stressed to sign up to a comp forum and 2) because some people on here know some VB.

WHY THE **** CAN'T VB COUNT OR AM I DOING SOMETHING STUPID?

My maths in a certain case should give me an answer of 20, instead I get 19.99999, I INT this number and hence problems arise, why oh why?! It ruins my whole program, one bit I bodge with a ROUND function and it sorts it but something else is more difficult and it gives me 0.000000001 and the checking is is this number > 0, it is according to VB, according to my maths it's 0!
 
Computers can't count in decimal! And find it very difficult to remember numbers with decimals in. INT(19.9) should give you the answer 20. Have you tried using Doubles (dim x as double) this (I think) holds decimal numbers as ints and remembers where the decimal point belongs. Use doubles for all the inputs and outputs of your equasion, and only INT() them at the end.

EDIT: Round(19.999, 0) should also give you 20... Sorry I was mistaken... INT(19.999) will give you 19 because the integer part of the number is 19.
 
lol
its pretty easy to fix... i remember back in ye olden college days when i had to sort that problem.

its nothing that Sam's Teach Yourself VB in x mins/days can't solve.

back to ur solution. i can't remember and i gave my vb book to another needy student so i can't look it up for u.
 
Alex, cheers for trying to help, I guess I didn't explain myself properly nor did I give you correct examples or context.

Maybe this will help:

My software adds 2 numbers together, takes one away and gives you an amount of change. This change has to be sorted out into denominations from $1 doller down to nickels, I have done that and fixed a problem with the ROUND function (when I don't think i needed to) I have tried changing "as single" to double for lots of my variables but it hasn't helped, Ah but I am not sure I have defined something, will check when I am back from asda. Anyway, the following should check that the numbers inputted in all 3 boxes are in 0.05 denominations:



'Check amounts inputted follow the rule of only accepting $0.05 increments'
'1) For deposited'
divisor = 0.05
quotient1 = Int(numdeposited / divisor)
remainder1 = numdeposited - quotient1 * divisor
If (remainder1) > 0 Then
picchange.Cls
MsgBox "Value must be in denominations of $0.05; please check the Amount Deposited value"
End If

'2) For previous amount'
quotient2 = Int(numprevious / divisor)
remainder2 = numprevious - quotient2 * divisor
If (remainder2) > 0 Then
picchange.Cls
MsgBox "Value must be in denominations of $0.05; please check the previous farecard value"
End If

'3) For value of fare being requested'
quotient3 = Int(numvalue / divisor)
remainder3 = numvalue - quotient3 * divisor
If (remainder3) > 0 Then
picchange.Cls
MsgBox "Value must be in denominations of $0.05; please check the new farecard value"
Else

End If


I have forgotten to define divisor value. This is silly because 1) I am not sure my breif requires this, I think it's maybe extra info to explain something else and 2) if blocks are 2 chapters away.
 
you could always write your own round function.

EDIT: Just read the above... so you've fixed the problem I thought you had... I have read through the above, and I'll have a think...
 
Last edited:
now again this may be way off beam, but it sounds like you need to use a little modulas (SP). the MOD of a number (I'm sure you know this already but it makes me look clever, unless I get it wrong) is the remainder of any devide. So for example 9/2=4.5 but 9mod2 = 1 because 2 goes into 9, 4 times and 2*4=8 , 9-8=1. Does this help at all or just make things more complicated?
 
Right, back from Asda. I am a little confused with the above, I have never used mod in that sense, only mod of a negative being a positive, so MOD (-3) = 3. I am not saying you're wrong at all, just I have never come accross that before and I am a little new to all this to see how it could help me :S

If I re-explain myself, I need to check that the inputs into three boxes are of 0.05 denominations. The way I though to do this is to divide the number by 0.05, if the number is an integer, it must be a denomination of 0.05, correct? I didn't know how to "ask" this in VB but I found some code in it which said:
divisor = 0.05
quotient3 = Int(numvalue / divisor)
remainder3 = numvalue - quotient3 * divisor
If (remainder2) > 0 Then
[msg box basically]


I considered the problem with VB not working with decimals so I changed the divisor to 1/20 instead of 0.05 and same problem. The error checking part as I call it works for a lot of numbers it's just for some, i.e. i think 1.15 doesn't, this gives a value of 0 mathematically but i think VB works it out to be 0.000001 or something, I tried taking 0.001 off each value but for some reason it didn't work.

Ou, what about this?

To work out if it's an integer:

do number/0.05 as before say it equals number2
get the integer of number2

now do (number/0.05) - int(number2) = remainder
IF remainder = 0
Then it must be divisible by 0.05, right?
 
Right, I think I MIGHT have sorted it by doing that, however:

Divisor = (1 / 20)
quotient1 = Int(numdeposited / Divisor)
quotient2 = (numdeposited / Divisor)
remainder1 = ((quotient1) - (quotient2))
If (remainder1) = 0 Then
picchange.Cls
MsgBox "Value must be in denominations of $0.05; please check the Amount Deposited value"
End If


At the moment, if it is divisible, i obviously get an error msg, is there a VB for "not equal to" or do I have to read "else" commands? lol

Cheers Alex for the help :)
 
I sort of see what you mean (sorry... I'm a little thick) Going back to my modulus (which I think is roughly the same as what you're doing) if you do this:

1.15 mod 0.05 you will get 0 because there is no remainder but if you did 1.16 mod 0.05 I don't know what you'd get possibly 0.01.

One think I might suggest, but it's not very eligent is:

If int(left(cStr(remainder3),1)), > 0 Then...

Working from the middle out:

Change the var Remainder3 to a string
Take the 1st char from the left of the string (which will be 0)
Force the whole statement to be an int.

Or you could just do this

If remainder3 then...

if remainder 3 is anything other than 0 then the statement will return true.

does this help
 
:D I sorted it, the following is an example:

Divisor = (1 / 20)
quotient1 = Int(numdeposited / Divisor)
quotient2 = (numdeposited / Divisor)
remainder1 = ((quotient1) - (quotient2))
If (remainder1) <> 0 Then
picchange.Cls
MsgBox "Value must be in denominations of $0.05; please check the Amount Deposited value"
End If

I think that's what you were getting at in the last part :) Cheers Alex, much appreciated - the feeling that somebody out there knows what I am going on about is good :)

I now have to go through it all and edit all the variables and such so that it all follows correct procedure to make my professor happy, i.e. making all DIMs correct, making sure all variables are numVar and such. I also need to comment on it throughout in a proper way, only just found out. Guess what I have also just realised, we're using VBA at uni in excel, this was done on "VB working model" - it can't be read by VBA, ********! lol Now I may have to rewrite it all in VBA on tuesday.

I think what I have done works anyway :)

Cheers
Paul
 
Ok heres how I would have done the same:

Dim Result as Integer

Result = (NumDeposited*100) MOD 5

If Result Then
msgbox "Enter in denominations of $0.05 dumb-ass"
Else
msgbox "Teacher Pet!"
End If
 
LOL@your msgboxes, your's is a lot simpler I don't know anything about the DIM thing except we're supposed to do it, was does double and single mean? My book doesn't explain it to me in words I understand yet. I assume it just means "Result must be an integer" for your case.

I might keep mine though just because it's easy for me to explain how I did it. The code from before was from a friend, I didn't even consider trying to think about it myself; I'm the dumbass, it only took me 2 mins to think about it lol.

Now i just wish I had a compiler to send to my parents so they can say "oooo!" lol
 
DIM is old skool basic... It basically comes from the day when all variables were arrays and had to be defined as a certain size (this is called dimentioning hence DIM) you use it to define variables and arrays (also re dim but just forget about that untill you need it)

Single and double are integer types (i believe) and thus telling you to use them was probably wrong... oops! single precision and double precision, I think single precision will hold a number up to 32K long, and a double about 64K, but with the advent of .NET these numbers are stupidly large. BTW I should have told you to use Floats (floating point) they are what I described doubles as being earlier.

In my case... it doesn't matter what the result is, if its 0 then the statement is false, if its 1.2345 its true if its 0.1 its true if its -1000000 its true. False is zero, and true is anything that isn't false. Except for Tristate stuff, but I'll not bother with that. But yes you are correct the answer to the equasion will always be an int, hense I have to multiply my input number by 100 to remove the decimal, but If I were to / the result by 100 I would get the number of cents were remaining, just like your solution.

Tip, where I told you to change everything to Double... change it to float! Sorry that was my mistake...

Another Tip is: www.devguru.com click on VBScript and there is a language index which documents most of the VBA functions (<-- sp that looks so wrong).

EDIT: I should mention that VBA and VBScript are not the same, and some things dont work in one that works in the other, but there is always an equivelant way of doing things.
 
Last edited:
Cheers Alex, I am still fiddling with it and am still stressed out over it. After these checks I have to break down the change into the following denominations, $1, quarters (25c), dimes (10c) and nickels, (5c), I used the following:

'Work out break down of change'
numDollars = Int(numChange)
picchange.Print numDollars; "dollars"
numRemainder1 = numChange - numDollars
numquarters = Int(numRemainder1 * 4)
picchange.Print numquarters; "Quarters"
numRemainder2 = numRemainder1 - (numquarters * 0.25)
numDimes = Int(Round(numRemainder2 * 10))
picchange.Print numDimes; "Dimes"
numRemainder3 = numRemainder2 - (numDimes * 0.1)
numNickels = Int(numRemainder3 * 20)
picchange.Print numNickels; "Nickels"

However, I just noticed on some values it gives me 1 too many dimes and -1 nickel lol. Previously I hadn't got the "ROUND" function but found it was giving me 1 dime and 2 nickels instead of 2 dimes in some cases. I thought it had fixed it but it hasn't :(

I must be really bad at all this :(
 
perhaps you could give an example using numbers, is it like this?

I enter $33.55 (which is allowed right)

the program then tells me that I gave it

$10 x 3
$1 x 3
$0.5 x 1
$0.05 x 1

OR

$20 x 1
$10 x1
$1 x 3
$0.5 x 1
$0.05 x 1

something like that?

Edit: Just re-read your post what you would get is this?

$1 x 33
$0.25 x 2
$0.05 x 1
 
Last edited:
your last bit is correct and it works for that value too, however, if you have a value of change of 10.95, i get:

10, 3, 2, -1

:(
 
OK

so here I again would multiply everything by 100 and work in cents.. I'm just trying to think how I would do that though.
 
Ok here's how I do it

Dim Dollar as Integer
Dim Quater as Integer
Dim Nickel as Integer

Dim Input as Float
Dim NewInput as Integer
Dim Remainder as Integer

newInput = Input*100

dollar = cInt(100/NewInput)
newInput = NewInput-(Dollar*100)

quater = cInt(25/NewInput)
newInput = newInput-(quater*25)

Nickel = cInt(NewInput/5)
newInput = NewInput-(nickel*5)

'You don't need this because you're entering in denoms of 0.05
Cent = cInt(NewInput/1)
newInput = NewInput-(cent*1)
 
Last edited:
Alex, cheers, will look at it in detail when I have finished this fluid flow question :)

Thanks for all your help, I owe you!
 
Back
Top