tags:

views:

700

answers:

3

I'm using the code below to convert the file size in bytes (test file is 31718 bytes) to KB (30.974609375 KB) but I want to display this to one decimal place (i.e. 30.9 KB). How would I do this in VB.NET?

New FileInfo(FileName).Length / 1024

Thanks

+4  A: 
Math.Round(New FileInfo(FileName).Length / 1024,1)
Mladen Mihajlovic
How can that be? In your example you gave these facts, size = 31718, divided by 1024 = 30.974609375, desired output = 30.9. The answer you said was perfect gives 31.0.
dbasnett
He was wrong about the 30.9. Rounding 30.97 will always round up, if it was 30.94 or less it would round down to 30.9.
Mladen Mihajlovic
But I'm sure he was just using it as an example...
Mladen Mihajlovic
+2  A: 

If it's just display output that you need it rounded for then use a format in the ToString

Double.ToString("0.0")
Robin Day
A: 

If you need it truncated, but not rounded (as your example implies), Then use Math.FLoor()

   Decimal val = Math.Floor(New FileInfo(FileName).Length / 102.4) / 10;
Charles Bretana