VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



How to round a byte amount into KB or MB (also KB/s) with two decimal places

by Luis Cantero (14 Submissions)
Category: Math/Dates
Compatability: Visual Basic 5.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (3 Votes)

Apart from what it says on the title, I will also demostrate how to calculate, based on a byte amount and a time period, the amount of KB/s (Kilobytes per second) of a transfer. The decimal separator (. or ,) is shown according to the regional settings.

Assumes
REMEMBER: It is good programming practice to declare your variables AND to make functions out of routines so that you can reuse or modify them easily in the future, this can also be applied to a group of functions and subs that should do something together, just put them in a module or class. Also, do not forget to always comment your code!

Rate How to round a byte amount into KB or MB (also KB/s) with two decimal places

'PURPOSE:  Rounds a Byte amount and returns KB with 2 decimal places
'INPUT:   Long: Byte amount
'OUTPUT:  String: Rounded KB amount
Function GetRoundedKB(lngByteAmount As Long) As String
  GetRoundedKB = FormatNumber(Int(lngByteAmount / 1024 * 100 + 0.5) / 100, 2)
End Function
'PURPOSE:  Rounds a Byte amount and returns, according to an elapsed time in seconds, KB/s with 2 decimal places
'INPUT:   Long: Byte amount
'OUTPUT:  String: Rounded KB/s amount
Public Function GetRoundedKBperS(lngByteAmount As Long, lngSecondsElapsed As Double) As String
  'Error check
  If lngSecondsElapsed <= 0 Then lngSecondsElapsed = 1
  GetRoundedKBperS = FormatNumber(Int(lngByteAmount / 1024 / lngSecondsElapsed * 100 + 0.5) / 100, 2)
End Function
'PURPOSE:  Rounds a Byte amount and returns MB with 2 decimal places
'INPUT:   Long: Byte amount
'OUTPUT:  String: Rounded MB amount
Public Function GetRoundedMB(lngByteAmount As Long) As String
  GetRoundedMB = FormatNumber(Int(lngByteAmount / 1048576 * 100 + 0.5) / 100, 2)
End Function

'Here's sample source code for an API that rounds a byte amount, 
'In my opinion, it is just too much for too little...:
Private Declare Function StrFormatByteSize Lib _
"shlwapi" Alias "StrFormatByteSizeA" (ByVal _
dw As Long, ByVal pszBuf As String, ByRef _
cchBuf As Long) As String
Public Function FormatKB(ByVal Amount As Long) _
As String
Dim Buffer As String
Dim Result As String
Buffer = Space$(255)
Result = StrFormatByteSize(Amount, Buffer, _
Len(Buffer))
If InStr(Result, vbNullChar) > 1 Then
FormatKB = Left$(Result, InStr(Result, _
vbNullChar) - 1)
End If
End Function

Download this snippet    Add to My Saved Code

How to round a byte amount into KB or MB (also KB/s) with two decimal places Comments

No comments have been posted about How to round a byte amount into KB or MB (also KB/s) with two decimal places. Why not be the first to post a comment about How to round a byte amount into KB or MB (also KB/s) with two decimal places.

Post your comment

Subject:
Message:
0/1000 characters