36,247,762,944 Bytes In Gb
I was refactoring some old code and came across the following line of code to convert bytes to GB.
So, to recap, we talked about Bits, how there are 8 bits in 1 byte, how there are 1000 bytes in 1 kilobyte, and 1000 kilobytes in 1 megabyte, and 1000 megabytes in 1 gigabyte, and 1000 gigabytes. Bytes to Gigabytes; 889 Kilojoules to Calories (IT) 3176 Kilojoules to Centigrade Heat Units.
Is there a better way to refactor the following piece of code?
Update
I meant to say bytes to Gigabytes. I gave wrong information.
13 Answers
hdoghmenIf exact precision is not important, use double:
Agree with Pavel here - there's not really any need to refactor this code... in fact, if this is the biggest problem in your codebase, I think you might be sitting on the most well-written software ever.
Rex MRex MThe original code is succinct, easy to read, and with reasonable variable names, self-documenting; I wouldn't change it.
If you absolutely must refactor, you could create a set of extension methods on the numeric types:
But unless your code does virtually nothing but convert bytes to kilobytes etc, all this will really do is clutter up Intellisense for no real gain.
technophiletechnophilePersonally I'd write it like this: decimal GB = KB / (1024 * 1024);
but there's really no need to refactor the code as written.
36 247 762 944 Bytes In Gb Conversion
Well, the formula is wrong (there's only about a million kilobytes in a gigabyte, not a thousand million) but, other than that, it's fine. Anyone used to working with these numbers will know what it means.
One thing I would watch out for (and I don't know if this is a problem with C#) is that a compiler may not be able to optimize the x/1024/1024
if x
is not a basic type. With C and integers, the compiler would quite easily turn that into a 'blindingly-fast-shift-right-by-20-bits' instruction.
If decimal is a class rather than a basic type, the compiler may have to do two divide operations. Whether this has any real impact on speed (or even whether it happens at all) is outside of my sphere of knowledge.
One thing I'd consider changing is the actual variable names. It makes no real difference to the compiled code but I prefer longer variable names rather than abbreviations, so I'd opt for kiloBytes/gigaBytes
or something like that. KB/GB
is too easy to get confused with constants, depending on your coding standards.
In theory, this is faster (precomputing the constant to do multiplication instead of division). It's probably not used often enough to matter, but just in case.
Kenan E. K.Kenan E. K.I wrote a small utility class that performs conversions between units, hth..
To make sure that the compiler pre-calculates the divisors:
Note that you are actually calculating GiB (gibibyte), not GB (gigabyte). If you really want to calculate GB, that would be:
GuffaGuffaI needed it the other way around, convert from 3rd party component literal size in words (e.g. '0 bytes', '1.1 MB') into generic size in bytes. so I used it this way:
This is a little improvement of the good JLopez's answer (please VOTE HIM, not me).Here you can choose to have or not the units indication and the kilo unit is written with the lowercase 'k' (the uppercase one is for Kelvin)