Link to home
Start Free TrialLog in
Avatar of Johnny
JohnnyFlag for United States of America

asked on

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);
}
Avatar of iboutchkine
iboutchkine

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

Avatar of Johnny

ASKER

im getting output like N2 KB

for a value of 3638

shouldnt it be around 3.4k or someplace around there??
???????????????
really confused
Avatar of Johnny

ASKER

i also noticed it rouinds off its not exact
Avatar of Johnny

ASKER

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

it should at least be 11k

go figure
ASKER CERTIFIED SOLUTION
Avatar of srcalc
srcalc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Johnny

ASKER

THANK YOU srcalc

works perfectly