Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Rounding and math.sqrt

0 views
Skip to first unread message

backw...@gmail.com

unread,
Mar 8, 2007, 4:28:23 PM3/8/07
to
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

Kerry Moorman

unread,
Mar 8, 2007, 5:19:11 PM3/8/07
to
Andy,

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

Kerry Moorman

backw...@gmail.com

unread,
Mar 9, 2007, 6:11:09 AM3/9/07
to
Thanks Kerry.

Using just an int seemed to fix that problem.

Andy

0 new messages