Didving UInt64 in vb.net

Hi

I have the numerator as a Unit64 number and likewise of denominator. How can I didvde them to get a 2 decimal number? FOr some reason vb.net doesntlike the / ,saying this operator is not defined for system.UInt64.

Thanls
LuckyLucksAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
This works fine in VB.Net 2005 Express:

        Dim l1 As System.UInt64 = 666
        Dim l2 As System.UInt64 = 34
        Dim d As Decimal = l1 / l2
        Dim strD As String = d.ToString("#0.00")
        Debug.Print(strD) ' 19.59

But in VB.Net 2003 it complains...and needed:

        Dim l1 As System.UInt64 = System.Convert.ToUInt64(666)
        Dim l2 As System.UInt64 = System.Convert.ToUInt64(34)
        Dim d As Decimal = System.Convert.ToDecimal(l1) / System.Convert.ToDecimal(l2)
        Dim strD As String = d.ToString("#0.00")
        Debug.WriteLine(strD) ' 19.59

Why are you using UInt64?

The Decimal value type represents decimal numbers ranging from:

    -79,228,162,514,264,337,593,543,950,335 to 79,228,162,514,264,337,593,543,950,335

which easily covers these numbers.

This worked fine in both VB.Net 2003 and VB.Net 2005:

        Dim l1 As Decimal = 666
        Dim l2 As Decimal = 34
        Dim d As Decimal = l1 / l2
        Dim strD As String = d.ToString("#0.00")
        Debug.Print(strD) ' 19.59
0
 
deightonprogCommented:
       Dim l1 As System.UInt64
        Dim l2 As System.UInt64

        l1 = System.Convert.ToUInt64(666)
        l2 = System.Convert.ToUInt64(34)




        Dim d As Decimal = CType(System.Convert.ToDouble(l1) / System.Convert.ToDouble(l2), Decimal)

0
 
deightonprogCommented:
basically you need to do the

System.Convert.ToDouble(l1)

to go to a double type which should always hold the unsigned 8 byte integer, this will be ok unless you are doing something with remainders, where precision errors might creep in.  But for two decimal places of accuracy, you will be ok.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.