how can we convert this java to vb.net: find from bytes - kb,mb,gb (if theres another way im ok with it)

can someone please tell me in vb.net how to convert bytes to kb,mb,gb from any bytes number

id like the format of

4 Bytes
or
4.6 KB
or
4.6 MB
or
4.6 GB

that type of output bare in mind it would only be one of them

below is the code i found for java if that helps any

thx
Johnny
aka Pern
------------------------------------------------------------------------------------------
public String getFileSize(long fsize){
     
     //Set things up
     DecimalFormat   df         = new DecimalFormat("0.00");
     String              sizeType   = new String();
     String          returnSize = new String();
     //Calculate file sizes
      long fsize_kb   = fsize / 1024;
     long fsize_mb   = fsize / 1024 / 1024;
     long fsize_gb   = fsize / 1024 / 1024 / 1024;
      //Pick the right size i.e KB, MB or GB
      if(fsize_kb < 1500 ){
          returnSize = df.format(fsize_kb);
          sizeType = "KB";
      }
      if(fsize_mb >= 1 && fsize_mb < 1000){
            returnSize = df.format(fsize_mb);
          sizeType = "MB";
     }
       if(fsize_gb >=1){
            returnSize = df.format(fsize_gb);
          sizeType = "GB";
     }
        //Return the size of the file with the right units....
       return(returnSize + sizeType);
}
JohnnyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

iboutchkineCommented:
Enum FormatMemorySizeUnits
    BestGuess
    Bytes
    Kilobytes
    Megabytes
    Gigabytes
End Enum


' convert a number of bytes into Kbytes, Megabytes, or Gigabytes

Function FormatMemorySize(ByVal value As Long, _
    ByVal unit As FormatMemorySizeUnits, Optional ByVal decimalDigits As _
    Integer = 2, Optional ByVal omitThousandSeps As Boolean = False) As String
    ' simple error checking
    If value < 0 Then Throw New ArgumentException("Value can't be negative")

    ' get the best unit, if required
    If unit = FormatMemorySizeUnits.BestGuess Then
        Select Case value
            Case Is < 1023
                unit = FormatMemorySizeUnits.Bytes
                decimalDigits = 0
            Case Is < 1024 * 1023
                unit = FormatMemorySizeUnits.Kilobytes
            Case Is < 1048576 * 1023
                unit = FormatMemorySizeUnits.Megabytes
            Case Else
                unit = FormatMemorySizeUnits.Gigabytes
        End Select
    End If

    ' evaluate the decimal value
    Dim val As Decimal
    Dim suffix As String

    Select Case unit
        Case FormatMemorySizeUnits.Bytes
            val = value
        Case FormatMemorySizeUnits.Kilobytes
            val = value / 1024
            suffix = "K"
        Case FormatMemorySizeUnits.Megabytes
            val = value / 1048576
            suffix = "M"
        Case FormatMemorySizeUnits.Gigabytes
            val = value / 1073741824
            suffix = "G"
    End Select

    ' get the string representation
    Dim format As String
    If omitThousandSeps Then
        format = "F" & decimalDigits.ToString
    Else
        format = "N" & decimalDigits.ToString
    End If

    Return val.ToString(format) & suffix
End Function

0
JohnnyAuthor Commented:
im getting output like N2 KB

for a value of 3638

shouldnt it be around 3.4k or someplace around there??
???????????????
really confused
0
JohnnyAuthor Commented:
i also noticed it rouinds off its not exact
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

JohnnyAuthor Commented:
11603 is converting to 2 KB as well it seams this dont work...

it should at least be 11k

go figure
0
srcalcCommented:
here is a conversion for that java code you gave, it seems to work well:

    Private Function getFileSize(ByVal fsize As Double) As String
        'Set things up
        Dim sizeType, returnSize As String
        'Calculate file sizes
        Dim fsize_kb, fsize_mb, fsize_gb As Double
        fsize_kb = fsize / 1024
        fsize_mb = fsize_kb / 1024
        fsize_gb = fsize_mb / 1024
        'Pick the right size i.e KB, MB or GB
        If fsize_kb < 1500 Then
            sizeType = "KB"
            returnSize = Format(fsize_kb, "0.00")
        ElseIf fsize_mb >= 1 And fsize_mb < 1000 Then
            sizeType = "MB"
            returnSize = Format(fsize_mb, "0.00")
        ElseIf fsize_gb >= 1 Then
            sizeType = "GB"
            returnSize = Format(fsize_gb, "0.00")
        Else
            sizeType = "KB"
            returnSize = Format(fsize_kb, "0.000")
        End If
        Return returnSize & sizeType
    End Function
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JohnnyAuthor Commented:
THANK YOU srcalc

works perfectly
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.