Attribute VB_Name = "modNum"
' -------------------------------------------------------------------------------
'
'   AVStoDVD - by MrC
'   File: modNum.bas
'   Purpose: General utility number-related functions
'
' -------------------------------------------------------------------------------


Option Explicit
Option Compare Binary
Option Base 0


Function IsNum(sNum As String, Optional DecimalSeparator As String = ".") As Boolean

    Dim sStr As String
    
    On Error GoTo Err_Handling
    
    If DecimalSeparator = "" Then DecimalSeparator = Mid(CStr(1.1), 2, 1)
    sStr = Replace(sNum, ".", DecimalSeparator)
    If Len(sStr) > 0 Then
        If Right(sStr, 1) <> DecimalSeparator Then
            IsNum = IsNumeric(sStr)
        End If
    End If

    Exit Function

Err_Handling:
    IsNum = False

End Function

Function IsMultipleN(ByVal Num As Single, N As Integer) As Boolean

    On Error GoTo Err_Handling
    
    Num = Int(Num)
    If Num / N = Int(Num / N) Then
        IsMultipleN = True
    Else
        IsMultipleN = False
    End If
    
    Exit Function

Err_Handling:
    IsMultipleN = False

End Function


Function IsEven(ByVal Input_Number As Long) As Boolean

    If Int(Input_Number / 2) = Input_Number / 2 Then
        IsEven = True
    Else
        IsEven = False
    End If

End Function


Function IsOdd(ByVal Input_Number As Long) As Boolean

    If Int(Input_Number / 2) <> Input_Number / 2 Then
        IsOdd = True
    Else
        IsOdd = False
    End If

End Function


Function RoundUp(ByVal Input_Number As Double) As Long

    If Input_Number > Int(Input_Number) Then
        RoundUp = Int(Input_Number) + 1
    Else
        RoundUp = Int(Input_Number)
    End If

End Function


Public Function Hex2Dec(ByVal Stringa_Hex As String) As Long
    
    Dim N As Long
    Dim LUNG As Long
    Dim VAL_TMP As Long
    Dim VAL_TOT As Long
    Dim Char As String

    Hex2Dec = 0

    LUNG = Len(Stringa_Hex)

    If LUNG < 1 Then Exit Function

    'On Error Resume Next
    VAL_TOT = 0

    For N = 0 To LUNG - 1
        Char = UCase$(Mid$(Stringa_Hex, LUNG - N, 1))
        Select Case Char
            Case "A"
                VAL_TMP = 10
            Case "B"
                VAL_TMP = 11
            Case "C"
                VAL_TMP = 12
            Case "D"
                VAL_TMP = 13
            Case "E"
                VAL_TMP = 14
            Case "F"
                VAL_TMP = 15
            Case Else
                VAL_TMP = Val(Char)
        End Select

        VAL_TOT = VAL_TOT + (VAL_TMP * (16 ^ N))
    Next N

    Hex2Dec = VAL_TOT
    
End Function

' Returns the fraction of a decimal number with the format: "Num/Den"
Public Function GetFraction(sDecimal As String) As String
    
    Dim dArray
    Dim lDenom As Double
    Dim lNumer As Double
    Dim A As Long
    Dim B As Long
    Dim t As Long
    
    sDecimal = Replace(sDecimal, ",", ".")
    dArray = Split(sDecimal, ".")
    If UBound(dArray) > 0 Then
        'Get the initial denominator: 1 * (10 ^ decimal portion length)
        lDenom = 10 ^ Len(dArray(1))
        'Get the initial numerator: integer portion of the number
        lNumer = Val(sDecimal) * lDenom
        ' Use the Euclidean algorithm to find the gcd
        A = Int(lNumer)
        B = Int(lDenom)
        While B <> 0
            t = B
            B = A Mod B
            A = t
        Wend
        GetFraction = lNumer / A & "/" & lDenom / A
    Else
        GetFraction = sDecimal & "/1"
    End If
    GetFraction = Replace(GetFraction, ",", ".")

End Function
