Posted  by  admin

36,247,762,944 Bytes In Gb

  1. 36 247 762 944 Bytes In Gb Conversion

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.

36 247 762 944 bytes in gb review

Is there a better way to refactor the following piece of code?

Update

I meant to say bytes to Gigabytes. I gave wrong information.

Michael Kniskern
Michael KniskernMichael Kniskern
12.7k61 gold badges151 silver badges217 bronze badges

13 Answers

hdoghmen
1,6561 gold badge19 silver badges25 bronze badges
JLopezJLopez

If 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 M
122k28 gold badges260 silver badges304 bronze badges

The 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.

36 247 762 944 bytes in gb 2technophiletechnophile
2,9431 gold badge16 silver badges23 bronze badges
Community
AZ_AZ_
26.8k24 gold badges135 silver badges188 bronze badges

Personally I'd write it like this: decimal GB = KB / (1024 * 1024); but there's really no need to refactor the code as written.

mattnewportmattnewport

36 247 762 944 Bytes In Gb Conversion

11.6k2 gold badges26 silver badges37 bronze badges

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.

paxdiablopaxdiablo
657k182 gold badges1306 silver badges1708 bronze badges

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.
12.3k1 gold badge36 silver badges47 bronze badges

I wrote a small utility class that performs conversions between units, hth..

JGUJGU

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:

GuffaGuffa
573k81 gold badges597 silver badges889 bronze badges

I 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:

Elad TalElad Tal

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)

tedebustedebus
LeoLeo
TJ-
10.1k8 gold badges50 silver badges77 bronze badges
36,247,762,944 Bytes In Gb
user3958481user3958481

Not the answer you're looking for? Browse other questions tagged c#refactoring or ask your own question.