Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Program to identify number should be in increments of 1000

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading