0

Hello I made a updater that downloads update files but I always used bytes as indication now
I found a way to convert it to megabytes,

it works good but one little problem it returns HUGE numbers for example,
a file that is 20MB will be displayed as : 20.26496724167345 MB

How do I make this number a bit shorter like 20.26MB

This is the code that converts it to mb :

    static double B2MB(long bytes)
    {
        return (bytes / 1024f) / 1024f;
    }

3 Answers3

3

20.26496724167345 isn't a huge number. It's just over 20. It's a long textual representation, but that's a different matter.

You need to look at where you're displaying the number. For example, you might want to use:

Console.WriteLine("{0:0.##}MB", value);

Or

Console.WriteLine("{0:F2}MB", value);

See custom numeric format strings and standard numeric format strings for more details.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

for using it in display you can always use String.Format method as in:

string mb = String.Format("{0:F2}MB", B2MB(bytes));

for just rounding it down you can use

Math.Round((bytes / 1024f) / 1024f, 2);
Mehran
  • 1,409
  • 2
  • 17
  • 27
Ahmed Masud
  • 21,655
  • 3
  • 33
  • 58
1

You can use Math.round to round to a specified number of digits. If you want two, as in this case, you use it like so: Math.Round(inputValue, 2);. Your code would look something like this:

static double B2MB(long bytes)
{
    return Math.Round((bytes / 1024f) / 1024f, 2);
}

NOTE: Because floating point numbers don't have infinite precision, this may result in something like 24.24999999999999999 instead of 24.25. This method is worth knowing, but if you're outputting it as a string, you should look at using formatting strings, as the other answers do.

Mike Precup
  • 4,148
  • 21
  • 41
  • Why lose data early? It's only the textual representation which needs truncation, so IMO it's when the OP converts from `double` to `string` that he should make the change. – Jon Skeet May 18 '13 at 15:53
  • If he's storing this value, then I agree, losing that data early isn't a good way to go. However, it's entirely possible he's looking to use the data immediately, or with something other than the Console. In either case, this should help him do so. The OP knows his situation better than we do, so leaving him with multiple options is probably best. – Mike Precup May 18 '13 at 15:58
  • The updater is a windows form app and it uses the file directly after it is downloaded and many people like to have clear simple numbers (The updater is for a new game that I am creating with some friends) – Menno van Leeuwen May 18 '13 at 16:00
  • Well string formatting can round instead of just truncating, too. This just fundamentally feels wrong to me - it's not like `Math.Round` can even *return* a value which has exactly two decimal digits. It can return the *closest* `double` value to that, but that's not the same thing. (If you suggested using `decimal` instead *and* rounded, that would be better IMO.) Personally I think it's important to distinguish between operations which are logically to do with the value itself, and operations which are only actually about textual conversions. This feels more like the latter to me. – Jon Skeet May 18 '13 at 16:00
  • I had no idea the string formatting rounded, thanks for the information. I've edited my post to say your answers should be preferred for string conversion. – Mike Precup May 18 '13 at 16:04