Rounding and math.sqrt

B

backwards15

Having a bit of trouble with rounding and math.sqrt.

I have below a small function that generates a 4 diget code from the
square root of the date.

Public Function Generate() As String
Dim n As Integer
Dim s As String = Today.ToString("ddMMyyyy")
n = Integer.Parse(s)
n = CInt(Math.Sqrt(n))
Return n.ToString("0000")
End Function

How ever when i have a day that has a decimal place higher than 5 the
last number is rounded up.

Example:

square root of the 12/03/2007 is 3468.7183512069699314940377884052
however using the above function it returns 3469

Anyone know how i can stop this rounding and just to read the 1st for
numbers?

Thanks in advance,

Andy
 
G

Guest

Andy,

Use the Visual Basic functions Int or Fix instead of CInt. Or use the Math
class's Floor method.

Kerry Moorman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top