validating a decimal number?

  • Thread starter Thread starter Howard
  • Start date Start date
H

Howard

Hi,
I'd like to validate two numbers being entered on a form.

One is to within 0.5 ( i..e. 6.5 or 7.0 is ok but 6.6 and 6.4 are not.)
and the other is to within 0.05 (i.e 6.05 and 6.40 are OK but 6.47 and 6.52
are not

I seem to be able to do the first one using
if (n - int(n) <> 0 ) and (n- int(n) <> 0.5) then msgbox "invalid"

but when I try almost the same thing with the second number using
if (n - int(n) <> 0 ) and (n- int(n) <> 0.05) then msgbox "invalid"

it fails.

The reason is that when n = 6.05 (i.e. a valid number), the value of n -
int(n) is held as 0.0499999999999 and so does not = 0.05.

what is the best way to get round this?

Howard
 
Wouldn't you just know it!
Three hours I've been fiddling with this and a few seconds after I give up
and post a message I come up with something that works.
I'll post it here in case anyone can see a flaw with it

private sub validate(s as string)
' validates a number to
nearest 0.05
a = CInt( s * 100) 'multiply by 100 and get rid of any
decimals
a = a Mod 5 'divide by 5 and take remaider -
will be zero if OK so...
If a <> 0 Then MsgBox "invalid"
end sub


Howard
 
Back
Top