Google-sheets – Google Sheets: Format number as Mhz/kHz, or MB, kB etc

conditional formattinggoogle sheets

I want to make a spreadsheet with Google Sheets to compare microprocessor's specifications. One column will show processor speed, another will show program space, another for RAM space and so on.

What I'd like is a custom number format that could display values to something along these lines:

Processor speed:
I enter into a cell:

  • "1,000,000,000" which then displays as "1GHz"
  • "20,000,000" which then displays as "20Mhz"
  • "16,500,000" which then displays as "16.5MHz"

EEPROM:
I enter into a cell:

  • "12,000" which then displays as "12kB
  • "765" which then displays as "765B"

How can I do this? I know some javascript programming, so perhaps I could do it that way. Perhaps there's a built in way of doing it though? I can't even find javascript API which lets me create custom number formatting rules other than the syntax characters that can be used to create a custom number format, as per this help document.

I will have one row for each microprocessor, and one column for a specification (e.g. one column for processor speed, one column for 32 bit/ 64 bit). I will want to sort them, so 20,000 should be sorted as being greater than 20, even though 20GHz sorted alphabetically would be less than 20Mhz.
I will want to be able to sort the columns

Edit:

To clarify, I want the formatted version (e.g. 1GHz) to appear in the same cell as the one I enter the number 1,000,000,000.

For example, All of column AB I use for processor speed. In any cell of column AB I enter a number, it will format it into GHz/MHz/kHz/Hz.

In other words, say I use column AE for the price of the microprocessor. To set up the formatting, I highlight the whole column "AE", then in the menu I go to "Format" > "Number" > "Currency". Then when I enter 3.45 into any cell in column "AE", the number re-formats to £3.45.

Best Answer

Look into the following functions: IF and CONCATENATE.

Taking the frequency as an example: you have one cell, B23 in this example, that contains your "raw" number. 2.2 GHz would be entered as 2200000000. Your sheet will be sortable by this column.

Then in C23, put this formula:

=if(B23>=1000000000,B23/1000000000,if(B23>=1000000,B23/1000000,if(B23>=1000,B23/1000,B23)))

In D23, this formula:

=if(B23>=1000000000,"GHz",if(B23>=1000000,"MHz",if(B23>=1000,"kHz","Hz")))

And in E23, this formula, which produces your formatted value:

=concatenate(C23," ",D23)

You don't actually need three cells. You can have just one cell, putting the IF functions in the parameters of the CONCATENATE function:

=concatenate(if(B23>=1000000000,B23/1000000000,if(B23>=1000000,B23/1000000,if(B23>=1000,B23/1000,B23)))," ", if(B23>=1000000000,"GHz",if(B23>=1000000,"MHz",if(B23>=1000,"kHz","Hz"))))

Using three cells just makes it easier to keep things straight, especially the first few times you use nested functions.

For values using binary prefixes (traditionally used for memory sizes), the factors change to 1024, 1024 squared, etc. Note that the prefix for 1000 is lowercase "k" while for 1024 it is uppercase "K"; M and G are always uppercase.