-
Nov 7th, 2013, 11:15 AM
#1
Unit suffix i.e. KB, MB, GB, etc.
The following extension makes it easy to format numbers with a suffix. Here is a sample that shows it in use:
Code:
Dim l As Long = Short.MaxValue
Debug.WriteLine(l.ToUnit())
Debug.WriteLine(l.ToUnit(2, Units.MB))
l = Integer.MaxValue
Debug.WriteLine(l.ToUnit())
Debug.WriteLine(l.ToUnit(2, Units.MB))
l = Long.MaxValue
Debug.WriteLine(l.ToUnit())
Debug.WriteLine(l.ToUnit(, Units.GB))
Debug.WriteLine(l.ToString("n0"))
which produces
32 KB
0.03 MB
2 GB
2,048.00 MB
8 EB
8,589,934,592 GB
9,223,372,036,854,775,807
Code:
Imports System.Runtime.CompilerServices
Module UnitExtension
Public Enum Units
B = 0
KB = 1 'kilo
MB = 2 'mega
GB = 3 'giga
TB = 4 'tera
PB = 5 'peta
EB = 6 'exa
ZB = 7 'zetta
YB = 8 'yotta
'add new values here as needed
Auto = -1 'last
End Enum
'compute max value of enum
Private ReadOnly maxU As Integer = [Enum].GetValues(GetType(Units)).Cast(Of Integer)().Max
'kFactor should be set according to your use
'see
'http://physics.nist.gov/cuu/Units/prefixes.html
'and
'http://physics.nist.gov/cuu/Units/binary.html
'kFactor should be 1000 or 1024
''' <summary>
''' converts a number to a string with a suffix taken from Public Enum Units
''' </summary>
''' <param name="theNumberToConvert">number to convert</param>
''' <param name="precision">number of decimals to show</param>
''' <param name="whichUnit">specify which Public Enum Units, Auto is default</param>
''' <param name="kFactor">1000 or 1024</param>
''' <returns>formatted string</returns>
''' <remarks></remarks>
<Extension()>
Public Function ToUnit(ByVal theNumberToConvert As Object, _
Optional ByVal precision As Integer = 0, _
Optional ByVal whichUnit As Units = Units.Auto, _
Optional kFactor As Integer = 1024) As String
Dim _aNumber As Double = CType(theNumberToConvert, Double) 'the number being converted
Dim _fmt As String = "n" & precision.ToString 'format string
Dim _unit As Integer
If whichUnit = Units.Auto Then 'auto unit
_unit = CInt(Math.Floor(Math.Log(_aNumber, Kfactor))) 'yes
If _unit > maxU Then '> max unit
_unit = maxU 'use max unit
End If
Else
_unit = whichUnit 'no, force unit
End If
Dim _numberOfUnits As Double = _aNumber / (Kfactor ^ _unit) 'calculate number of units
Return String.Format("{0} {1}", _numberOfUnits.ToString(_fmt), CType(_unit, Units))
End Function
<Extension()>
Public Function ToUnit(ByVal theNumberToConvert As Object, _
fromUnit As Units, _
Optional ByVal precision As Integer = 0, _
Optional ByVal whichUnit As Units = Units.Auto, _
Optional kFactor As Integer = 1024) As String
Dim _aNumber As Double = CType(theNumberToConvert, Double) * (kFactor ^ fromUnit) 'the number being converted
Dim _fmt As String = "n" & precision.ToString 'format string
Dim _unit As Integer
If whichUnit = Units.Auto Then 'auto unit
_unit = CInt(Math.Floor(Math.Log(_aNumber, Kfactor))) 'yes
If _unit > maxU Then '> max unit
_unit = maxU 'use max unit
End If
Else
_unit = whichUnit 'no, force unit
End If
Dim _numberOfUnits As Double = _aNumber / (Kfactor ^ _unit) 'calculate number of units
Return String.Format("{0} {1}", _numberOfUnits.ToString(_fmt), CType(_unit, Units))
End Function
End Module
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|