You will find many of the common mathematical functions are built into the Math. namespace. This page offers a few additional functions that are sometimes useful and provides examples of some lesser-known language features. For simplicity, I have included any required structures immediately after each function, however, structures are normally located after the End Class statement.

Extract decimal part of a number
``` Private Shared Function Get_Decimal_Part(ByVal dec As Double) As Double Return (dec - Math.Floor(dec)) End Function ```

Convert Integer to Hex
``` Private Shared Function IntegerToHex(i As Integer) As String Return Hex(i) End Function ```

Convert Hex to Integer
``` Private Shared Function HexToInteger(h As String) As Integer Return Convert.ToInt32(h.ToLower, 16) End Function ```

Distance between two points
``` Private Shared Function Distance(x1 As Double, y1 As Double, x2 as Double, y2 as Double) As Double Return Math.Sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2 ) *(y1 - y2))) End Function ```

Is Number a Prime
Caution - this only works for small numbers (up to a few thousand) because it is a slow process

``` Private Shared Function Is_Prime_Number(n As Integer) As Boolean ' Euclidian method much faster for huge numbers but on rare occassions, it gives wrong answer ' Euclidian method involves very advanced mathematics ' This function is not suitable for very large numbers Dim ret As Boolean = True n = Math.Abs(n) If n < 4 Then Return True If n Mod 2 = 0 Then Return False Dim s As Integer = CInt(Math.Floor(Math.Sqrt(n))) For i As Integer = 3 To s Step 2 If n Mod s = 0 Then ret = False Exit For End If Next Return ret End Function ```

Sum (Total)
``` Private Shared Function Sum(ListV As List(Of Double)) As Double Return ListV.Sum End Function ```

Mean (Average)
``` Private Shared Function Sum(ListV As List(Of Double)) As Double Return ListV.Average End Function ```

Median
``` Private Shared Function Median(ListV As List(Of Double)) As Double Dim ret As Double ListV.Sort() ret = ListV.Item((ListV.Count - 1) \ 2) If ListV.Count Mod 2 = 0 Then ret = ListV.Item((ListV.Count - 1) \ 2) ret += ListV.Item(ListV.Count \ 2) ret /= 2 End If Return ret End Function ```

Mode
``` Private Shared Function Mode(ListValues As List(Of Double)) As Double Return ListValues.GroupBy(Function(n) n).OrderByDescending(Function(g) g.Count).Select(Function(g) g.Key).FirstOrDefault End Function ```

Variance
``` Private Shared Function Variance(ListV As List(Of Double)) As Double Dim ret As Double = 0 For Each d As Double In ListV ret += Math.Pow(d - ListV.Average, 2) Next ret /= ListV.Count Return ret End Function ```

Standard Deviation
``` Private Shared Function StandardDeviation(ListV As List(Of Double)) As Double Dim ret As Double = 0 For Each d2 As Double In ListV ret += Math.Pow(d2 - ListV.Average, 2) Next ret /= ListV.Count Return Math.Sqrt(ret) End Function ```

Percentile Value
``` Private Function PercentileValue(ListV As List(Of Double), percent As Double) As Double Dim ret As Double = 0 ListV.Sort() Dim i As Integer = Math.Round(ListV.Count * percent \ 100) - 1 If i >= 0 Then ret = ListV(i) End If Return ret End Function ```

Percentile
``` Private Shared Function Percentile(listV As List(Of Double), valu As Double) As Double listV.Sort() Dim ret As Double = 0 For i As Integer = 0 To listV.Count - 1 If listV.Item(i) >= valu Then Exit For End If ret = 100 * (i + 1) / listV.Count Next Return ret End Function ```

Greatest Common Denominator
VB.Net has a namespace dedicated to BigInteger arithmatic. If you are using huge mumbers or learning about basic encryption, you may find some answers in the Big Integer documentation.

``` Private Shared Function Greatest_Common_Denominator(n1 As Integer, n2 As Integer) As Integer If n1 = 0 Or n2 = 0 Then Return 0 Dim min1 As Integer = Math.Min(n1, n2) Dim max1 As Integer = Math.Max(n1, n2) Dim lastMax1 As Integer = 0 While max1 <> lastMax1 lastMax1 = max1 max1 = max1 Mod min1 End While Return max1 End Function ```

Lowest Common Multiple
This function uses the Greatest_Common_Denominator function, the approriate code is immediately above this example

``` Private Shared Function Lowest_Common_Multiple(n1 As Integer, n2 As Integer) As Integer Return (n1 * n2) \ Greatest_Common_Denominator(n1, n2) End Function ```

Degrees Minutes and Seconds
``` Public Shared Function DegMinSecToDeg(d As Double, Optional m As Double = 0, Optional s As Double = 0) As Double Return d + (m / 60) + (s / 3600) End Function ' Public Shared Function DegToDegMin(d As Double) As IntegerDouble Dim ret As IntegerDouble ret.i1 = CInt(Int(d)) ret.d1 = (d - CInt(Math.Floor(d))) * 60 Return ret End Function Public Structure IntegerDouble Dim i1 As Integer Dim d1 As Double End Structure ' Public Shared Function DegToDegMinSec(d As Double) As IntegerIntegerDouble Dim ret As IntegerIntegerDouble ret.i1 = CInt(Int(d)) d = (d - ret.i1) * 60 ret.i2 = CInt(Int(d)) ret.d1 = (d - ret.i2) * 60 Return ret End Function Public Structure IntegerIntegerDouble Dim i1 As Integer Dim i2 As Integer Dim d1 As Double End Structure ```

Temperature
``` Public Shared Function Celcius_To_Farenheit(ByVal value As Double) As Double Return ((9 * value) + 160) / 5 End Function ' Public Shared Function Farenheit_To_Celcius(ByVal value As Double) As Double Return ((5 * value) - 160) / 9 End Function ```

Convert Percentage to Fraction
``` Private Shared Function GetFraction(percent As Integer) As Fraction Dim frac1 As New Fraction Dim i As Integer = 2 frac1.top = percent frac1.bottom = 100 While i <= frac1.bottom If (frac1.top Mod i = 0) And (frac1.bottom Mod i = 0) Then frac1.top \= i ' the \ means integer division frac1.bottom \= i ' the \ means integer division Else i += 1 End If End While Return frac1 End Function Public Structure Fraction Dim top As Integer Dim bottom As Integer End Structure ```

Convert Fraction to Percentage
``` Private Function GetPercent(fracTop As Integer, fracBottom as Integer) As Double Return 100 * fracTop / fracBottom End Function ```
DigitalDan.co.uk ... Hits = 201