Home > Software engineering >  Program to identify number should be in increments of 1000
Program to identify number should be in increments of 1000

Time:12-23

In VB.NET, We have one text field and we are taking decimal number as input.

Example: Input number is 7000.01 and we are dividing it by 1000. We want input number should be in increments of 1000.

Code snippet:

vAnswersErrs(MINADDINDEX) is input number.

If CLng(vAnswers(MINADDINDEX) Mod 1000) = 0 Then
     fDldErrors = True
     vAnswersErrs(MINADDINDEX) = "Minimum amount must be in increments of 1000"
End If
If Int(CLng(vAnswers(MINADDINDEX)) / 1000) <> (CLng(vAnswers(MINADDINDEX)) / 1000) Then
     fDldErrors = True
     vAnswersErrs(MINADDINDEX) = "Maximum amount must be in increments of 1000"
End If 

Currently it is allowing decimal points, but it should not.

CodePudding user response:

Since you are using CLng you will not be able to detect decimals. Use CDec instead. CDec returns the number as Decimal. This is better than Double in this context, as Double tends to produce conversion errors. E.g., CDbl("7000") could be returned as 6999.9999...

Dim answer As Decimal = CDec("7000.1")
If answer Mod 1000 = 0 Then
    ...
End If

I don't know what the type of vAnswersErrs is, but if it is an Integer or Long, this will not work. If it is a String, then CDec(vAnswers(MINADDINDEX)) Mod 1000 = 0 will work.

  • Related