Johnny
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);
}
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);
}
ASKER
im getting output like N2 KB
for a value of 3638
shouldnt it be around 3.4k or someplace around there??
???????????????
really confused
for a value of 3638
shouldnt it be around 3.4k or someplace around there??
???????????????
really confused
ASKER
i also noticed it rouinds off its not exact
ASKER
11603 is converting to 2 KB as well it seams this dont work...
it should at least be 11k
go figure
it should at least be 11k
go figure
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
THANK YOU srcalc
works perfectly
works perfectly
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.Best
Select Case value
Case Is < 1023
unit = FormatMemorySizeUnits.Byte
decimalDigits = 0
Case Is < 1024 * 1023
unit = FormatMemorySizeUnits.Kilo
Case Is < 1048576 * 1023
unit = FormatMemorySizeUnits.Mega
Case Else
unit = FormatMemorySizeUnits.Giga
End Select
End If
' evaluate the decimal value
Dim val As Decimal
Dim suffix As String
Select Case unit
Case FormatMemorySizeUnits.Byte
val = value
Case FormatMemorySizeUnits.Kilo
val = value / 1024
suffix = "K"
Case FormatMemorySizeUnits.Mega
val = value / 1048576
suffix = "M"
Case FormatMemorySizeUnits.Giga
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