''' <summary>
''' Return specified Double # (NumDbl) as String using specified Number Format String (FormatStr,
''' Default = "N0") and Format Provider (FmtProvider, Default = Nothing) followed by space and,
''' if NumDbl = 1, the specified Singular Unit Name (SglUnitStr), else the Plural Unit Name
''' (PluralUnitStr).
''' </summary>
''' <param name="NumDbl"></param>
''' <param name="SglUnitStr"></param>
''' <param name="PluralUnitStr"></param>
''' <param name="FormatStr"></param>
''' <param name="FmtProvider"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function PluralizeUnitsStr( _
ByVal NumDbl As Double, _
ByVal SglUnitStr As String, _
ByVal PluralUnitStr As String, _
Optional ByVal FormatStr As String = "N0", _
Optional ByVal FmtProvider As System.IFormatProvider = Nothing _
) As String
PluralizeUnitsStr = NumDbl.ToString(FormatStr, FmtProvider) & " "
Dim RsltUnitStr As String
If NumDbl = 1 Then
RsltUnitStr = SglUnitStr
Else
RsltUnitStr = PluralUnitStr
End If
PluralizeUnitsStr &= RsltUnitStr
End Function
''' <summary>
''' Info about a # Unit.
''' </summary>
''' <remarks></remarks>
Public Class clsNumUnitInfoItem
''' <summary>
''' Name of a Singular Unit (i.e. "day", "trillion", "foot")
''' </summary>
''' <remarks></remarks>
Public UnitSglStr As String
''' <summary>
''' Name of a Plural Unit (i.e. "days", "trillion", "feet")
''' </summary>
''' <remarks></remarks>
Public UnitPluralStr As String
''' <summary>
''' # of Units to = 1 of Next Higher (aka Parent) Unit (i.e. 24 "hours", 1000 "million",
''' 5280 "feet")
''' </summary>
''' <remarks></remarks>
Public UnitsInParentInt As Integer
End Class ' -- clsNumUnitInfoItem
Dim TimeLongEnUnitInfoItms As clsNumUnitInfoItem() = { _
New clsNumUnitInfoItem With {.UnitSglStr = "day", .UnitPluralStr = "days", .UnitsInParentInt = 1}, _
New clsNumUnitInfoItem With {.UnitSglStr = "hour", .UnitPluralStr = "hours", .UnitsInParentInt = 24}, _
New clsNumUnitInfoItem With {.UnitSglStr = "minute", .UnitPluralStr = "minutes", .UnitsInParentInt = 60}, _
New clsNumUnitInfoItem With {.UnitSglStr = "second", .UnitPluralStr = "seconds", .UnitsInParentInt = 60}, _
New clsNumUnitInfoItem With {.UnitSglStr = "millisecond", .UnitPluralStr = "milliseconds", .UnitsInParentInt = 1000} _
} ' -- Dim TimeLongEnUnitInfoItms
Dim TimeShortEnUnitInfoItms As clsNumUnitInfoItem() = { _
New clsNumUnitInfoItem With {.UnitSglStr = "day", .UnitPluralStr = "days", .UnitsInParentInt = 1}, _
New clsNumUnitInfoItem With {.UnitSglStr = "hr", .UnitPluralStr = "hrs", .UnitsInParentInt = 24}, _
New clsNumUnitInfoItem With {.UnitSglStr = "min", .UnitPluralStr = "mins", .UnitsInParentInt = 60}, _
New clsNumUnitInfoItem With {.UnitSglStr = "sec", .UnitPluralStr = "secs", .UnitsInParentInt = 60}, _
New clsNumUnitInfoItem With {.UnitSglStr = "msec", .UnitPluralStr = "msecs", .UnitsInParentInt = 1000} _
} ' -- Dim TimeShortEnUnitInfoItms
''' <summary>
''' Convert a specified Double Number (NumDbl) to a long (aka verbose) format (i.e. "1 day,
''' 2 hours, 3 minutes, 4 seconds and 567 milliseconds") with a specified Array of Time Unit
''' Info Items (TimeUnitInfoItms), Conjunction (ConjStr, Default = "and"), Minimum Unit Level
''' Shown (MinUnitLevInt) (0 to TimeUnitInfoItms.Length - 1, -1=All), Maximum Unit Level Shown
''' (MaxUnitLevInt) (-1=All), Maximum # of Unit Levels Shown (MaxNumUnitLevsInt) (1 to 0 to
''' TimeUnitInfoItms.Length - 1, 0=All) and Round Last Shown Units Up Flag (RoundUpBool).
''' Suppress leading 0 Unit Levels.
''' </summary>
''' <param name="NumDbl"></param>
''' <param name="NumUnitInfoItms"></param>
''' <param name="ConjStr"></param>
''' <param name="MinUnitLevInt"></param>
''' <param name="MaxUnitLevInt"></param>
''' <param name="MaxNumUnitLevsInt"></param>
''' <param name="RoundUpBool"></param>
''' <param name="FormatStr"></param>
''' <param name="FmtProvider"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function NumToLongStr( _
ByVal NumDbl As Double, _
ByVal NumUnitInfoItms As clsNumUnitInfoItem(), _
Optional ByVal ConjStr As String = "and", _
Optional ByVal MinUnitLevInt As Integer = -1, _
Optional ByVal MaxUnitLevInt As Integer = -1, _
Optional ByVal MaxNumUnitLevsInt As Integer = 0, _
Optional ByVal RoundUpBool As Boolean = False, _
Optional ByVal FormatStr As String = "N0", _
Optional ByVal FmtProvider As System.IFormatProvider = Nothing _
) As String
NumToLongStr = ""
Const TUnitDelimStr As String = ", "
If (MinUnitLevInt < -1) OrElse (MinUnitLevInt >= NumUnitInfoItms.Length) Then
Throw New Exception("Invalid MinUnitLevInt: " & MaxUnitLevInt)
End If
If (MaxUnitLevInt < -1) OrElse (MaxUnitLevInt >= NumUnitInfoItms.Length) Then
Throw New Exception("Invalid MaxDetailLevelInt: " & MaxUnitLevInt)
End If
If (MaxNumUnitLevsInt < 0) OrElse (MaxNumUnitLevsInt > NumUnitInfoItms.Length) Then
Throw New Exception("Invalid MaxNumUnitLevsInt: " & MaxNumUnitLevsInt)
End If
Dim PrevNumUnitsDbl As Double = NumDbl
Dim CurrUnitLevInt As Integer = -1
Dim NumUnitLevsShownInt As Integer = 0
For Each UnitInfoItem In NumUnitInfoItms
CurrUnitLevInt += 1
With UnitInfoItem
Dim CurrNumUnitsDbl As Double = PrevNumUnitsDbl * .UnitsInParentInt
Dim CurrTruncNumUnitsInt As Integer = Math.Truncate(CurrNumUnitsDbl)
PrevNumUnitsDbl = CurrNumUnitsDbl
If CurrUnitLevInt < MinUnitLevInt Then Continue For
PrevNumUnitsDbl -= CurrTruncNumUnitsInt
'If (CurrUnitLevInt > TimeUnitInfoItms.Length) _
' OrElse _
' ( _
' (CurrUnitLevInt > MaxUnitLevInt) AndAlso _
' (MaxUnitLevInt <> -1) _
' ) _
' OrElse _
' ( _
' (NumUnitLevsShownInt + 1 > MaxNumUnitLevsInt) AndAlso _
' (MaxNumUnitLevsInt <> 0) _
' ) Then Exit For
If (CurrUnitLevInt = (NumUnitInfoItms.Length - 1)) OrElse _
(CurrUnitLevInt = MaxUnitLevInt) OrElse _
((NumUnitLevsShownInt + 1) = MaxNumUnitLevsInt) Then
If NumUnitLevsShownInt > 0 Then
Dim TUnitDelimStrLenInt As Integer = TUnitDelimStr.Length
NumToLongStr = NumToLongStr.Remove( _
NumToLongStr.Length - TUnitDelimStrLenInt, _
TUnitDelimStrLenInt)
NumToLongStr &= " " & ConjStr & " "
End If
Dim CurrNunUnitsRoundedInt As Integer
If RoundUpBool Then
If CurrNumUnitsDbl <> CurrTruncNumUnitsInt Then
CurrNunUnitsRoundedInt = CurrTruncNumUnitsInt + 1
Else
CurrNunUnitsRoundedInt = CurrTruncNumUnitsInt
End If
Else
CurrNunUnitsRoundedInt = Math.Round( _
value:=CurrNumUnitsDbl, mode:=MidpointRounding.AwayFromZero)
End If
NumToLongStr &= _
PluralizeUnitsStr(CurrNunUnitsRoundedInt, _
.UnitSglStr, .UnitPluralStr, FormatStr, FmtProvider)
Exit For
Else ' -- Not (MaxUnitLevInt or MaxNumUnitLevsInt)
If NumUnitLevsShownInt > 0 OrElse CurrTruncNumUnitsInt <> 0 Then
NumToLongStr &= _
PluralizeUnitsStr(CurrTruncNumUnitsInt, _
.UnitSglStr, .UnitPluralStr, FormatStr, FmtProvider) & _
TUnitDelimStr
NumUnitLevsShownInt += 1
End If
End If ' -- Else Not (MaxUnitLevInt or MaxNumUnitLevsInt)
End With ' -- UnitInfoItem
Next UnitInfoItem
End Function
''' <summary>
''' Call NumToLongStr with a specified TimeSpan's (TS) TotalDays.
''' </summary>
''' <param name="TS"></param>
''' <param name="TimeUnitInfoItms"></param>
''' <param name="ConjStr"></param>
''' <param name="MinUnitLevInt"></param>
''' <param name="MaxUnitLevInt"></param>
''' <param name="MaxNumUnitLevsInt"></param>
''' <param name="RoundUpBool"></param>
''' <param name="FormatStr"></param>
''' <param name="FmtProvider"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function TimeSpanToStr( _
ByVal TS As TimeSpan, _
ByVal TimeUnitInfoItms As clsNumUnitInfoItem(), _
Optional ByVal ConjStr As String = "and", _
Optional ByVal MinUnitLevInt As Integer = -1, _
Optional ByVal MaxUnitLevInt As Integer = -1, _
Optional ByVal MaxNumUnitLevsInt As Integer = 0, _
Optional ByVal RoundUpBool As Boolean = False, _
Optional ByVal FormatStr As String = "N0", _
Optional ByVal FmtProvider As System.IFormatProvider = Nothing _
) As String
Return NumToLongStr( _
NumDbl:=TS.TotalDays, _
NumUnitInfoItms:=TimeUnitInfoItms, _
ConjStr:=ConjStr, _
MinUnitLevInt:=MinUnitLevInt, _
MaxUnitLevInt:=MaxUnitLevInt, _
MaxNumUnitLevsInt:=MaxNumUnitLevsInt, _
RoundUpBool:=RoundUpBool, _
FormatStr:=FormatStr, _
FmtProvider:=FmtProvider _
)
End Function
''' <summary>
''' Call TimeSpanToStr with TimeLongEnUnitInfoItms.
''' </summary>
''' <param name="TS"></param>
''' <param name="MinUnitLevInt"></param>
''' <param name="MaxUnitLevInt"></param>
''' <param name="MaxNumUnitLevsInt"></param>
''' <param name="RoundUpBool"></param>
''' <param name="FormatStr"></param>
''' <param name="FmtProvider"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function TimeSpanToLongEnStr( _
ByVal TS As TimeSpan, _
Optional ByVal MinUnitLevInt As Integer = -1, _
Optional ByVal MaxUnitLevInt As Integer = -1, _
Optional ByVal MaxNumUnitLevsInt As Integer = 0, _
Optional ByVal RoundUpBool As Boolean = False, _
Optional ByVal FormatStr As String = "N0", _
Optional ByVal FmtProvider As System.IFormatProvider = Nothing _
) As String
Return TimeSpanToStr( _
TS:=TS, _
TimeUnitInfoItms:=TimeLongEnUnitInfoItms, _
MinUnitLevInt:=MinUnitLevInt, _
MaxUnitLevInt:=MaxUnitLevInt, _
MaxNumUnitLevsInt:=MaxNumUnitLevsInt, _
RoundUpBool:=RoundUpBool, _
FormatStr:=FormatStr, _
FmtProvider:=FmtProvider _
)
End Function