Last active
March 1, 2026 18:40
-
-
Save seb26/e3837b36ddb5c4b945e3ef40c4e48b19 to your computer and use it in GitHub Desktop.
Revisions
-
seb26 revised this gist
Mar 30, 2019 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,9 +14,9 @@ You need to use a helper cell for this to work. This means you need to have the ### Why use it If your sheet contains data concerning file sizes, systems and other information storage, then often representing it in binary units (or IEC units) is the most correct format for that use case. However, it depends on the operating system or filesystem which may be relevant. For example, if your spreadsheet contains records of file transfers made on macOS, it will be more accurate and closer to Finder's approximations if you represent in decimal format (i.e. not using this formula). This is because macOS since 2009's Snow Leopard [has used the decimal format](https://support.apple.com/en-us/HT201402) to display sizes. Conversely, Windows will display file sizes in binary format, but labelled as 'KB' rather than 'KiB'. So if your use case predominately involves Windows systems, it may make sense to use this formula. A lot of confusion stems from using the units 'KB', 'MB', and so on, to incorrectly represent binary units, which should be denominated as 'KiB' instead. Further confusion stems from whether to use decimal or binary units in the first place. See https://en.wikipedia.org/wiki/Binary_prefix for more background on this topic. -
seb26 revised this gist
Mar 30, 2019 . 1 changed file with 3 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,18 +14,11 @@ You need to use a helper cell for this to work. This means you need to have the ### Why use it If your sheet contains data concerning file sizes, systems and other information storage, then often representing it in binary units (or IEC units) is the most correct format for that use case. However, it depends on the operating system or filesystem which may be relevant. For example, if your spreadsheet contains records of file transfers made on macOS, it will be more accurate and closer to Finder's approximations if you represent in decimal format (i.e. not using this formula). This is because macOS since 2009's Snow Leopard [has used the decimal format](https://support.apple.com/en-us/HT201402) to display sizes. A lot of confusion stems from using the units 'KB', 'MB', and so on, to incorrectly represent binary units, which should be denominated as 'KiB' instead. Further confusion stems from whether to use decimal or binary units in the first place. See https://en.wikipedia.org/wiki/Binary_prefix for more background on this topic. ### Experiment live with the formula -
seb26 revised this gist
Mar 30, 2019 . No changes.There are no files selected for viewing
-
seb26 revised this gist
Mar 30, 2019 . 1 changed file with 5 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,7 +18,6 @@ If your sheet contains data concerning file sizes, systems and other information If you represent your Byte amounts in decimal format (KB, MB, etc), the values are arguably incorrect in the context of these systems. A lot of confusion stems from using the letters 'KB' to represent binary units which should be denominated as 'KiB' instead. See https://en.wikipedia.org/wiki/Binary_prefix#Inconsistent_use_of_units for more background on this topic. Example table showing differences between the two numbering systems. | Bytes | Decimal | Binary | @@ -28,6 +27,8 @@ Example table showing differences between the two numbering systems. | 4,194,300 | 4.19 MB | 4.00 MiB | | 1,153,156,984 | 1.15 GB | 1.07 GiB | ### Experiment live with the formula [**Google Sheets experimental table**](https://docs.google.com/spreadsheets/d/1t6BxbeWjMt5qOM0idOwj9pATjsL--5ZNIEtw8LHwx8I/edit?usp=sharing) ### Scripts available on the Internet @@ -41,17 +42,15 @@ There are a number of scripts, formulas and snippets available on StackOverflow, * https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript/18650828#18650828 – I sourced the math for this formula from this, and the below Google Apps script ### Google Apps Script It is also possible to use a JavaScript function directly in Sheets which will achieve the same effect but with the math written in JavaScript. [**A custom Google Apps Script: FORMATBYTES()**](https://script.google.com/d/1eZ3AoZBc-9Cj5QaPiCwR2G5kn6zu2-zv_9GSyxN6qh9Qam4EZjt_RQVX/edit?usp=sharing) You can copy the contents of this function, choose File > New > Script file, and paste it in. That should enable you to use `FORMATBYTES()` as a function immediately in your Sheets document. This is cleaner than pasting an entire formula. **Downside**: this function is noticeably slower. Google Sheets appears to need about a second to retrieve the script and process its value. This means when your Bytes values update, the cell will show "Loading..." for a second and then finally give the value. It was sufficiently long enough to annoy me to create the above formula. To me, there was also nothing special about the math behind the conversion that mean that Sheets, a program designed for calculations, could not handle. ### Why do we have to do this? Google sorely needs to add KiB, MiB and so on, as actual units available as Number formats. This would mean that a user could type bytes into one cell and format that same cell to receive KiB immediately in that same cell. This absence of functionality is astonishing given the wide range of other scientific units that are available. -
seb26 revised this gist
Mar 30, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ You need to use a helper cell for this to work. This means you need to have the 3. Replace every mention of `A2` with the appropriate cell | |A|B| |-|-|-| |1|bytes value|**formula**| |2|2048|2 KiB| -
seb26 revised this gist
Mar 30, 2019 . 1 changed file with 35 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,19 @@ This formula can be used in Google Sheets to take a value in bytes and then represent it in a human-readable format with binary units (KiB, MiB, GiB, and so on). You need to use a helper cell for this to work. This means you need to have the bytes in one cell, and your formatted units in another cell. ### How to use it 1. Copy the formula above 2. Paste the formula into your helper cell 3. Replace every mention of `A2` with the appropriate cell | |A|B| |-|-|-|-| |1|bytes value|**formula**| |2|2048|2 KiB| ### Why use it If your sheet contains data concerning file sizes, systems and other information storage, then representing it in binary units (or IEC units) is absolutely essential and the most correct format for that use case. If you represent your Byte amounts in decimal format (KB, MB, etc), the values are arguably incorrect in the context of these systems. A lot of confusion stems from using the letters 'KB' to represent binary units which should be denominated as 'KiB' instead. See https://en.wikipedia.org/wiki/Binary_prefix#Inconsistent_use_of_units for more background on this topic. @@ -14,6 +28,8 @@ Example table showing differences between the two numbering systems. | 4,194,300 | 4.19 MB | 4.00 MiB | | 1,153,156,984 | 1.15 GB | 1.07 GiB | [**Google Sheets experimental table**](https://docs.google.com/spreadsheets/d/1t6BxbeWjMt5qOM0idOwj9pATjsL--5ZNIEtw8LHwx8I/edit?usp=sharing) ### Scripts available on the Internet There are a number of scripts, formulas and snippets available on StackOverflow, Google discussion forums and other sites, but the majority of them represent data sizes in decimal instead of binary. @@ -22,4 +38,22 @@ There are a number of scripts, formulas and snippets available on StackOverflow, * https://gist.github.com/loganvolkers/933af8513ed8c2268f59c85a31761a43 #### Binary units * https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript/18650828#18650828 – I sourced the math for this formula from this, and the below Google Apps script ### Google Apps Script It is also possible to use a JavaScript function directly in Sheets which will take your bytes, perform the same math and will produce a text string with ` KiB` on the end. [**A custom Google Apps Script: FORMATBYTES()**](https://script.google.com/d/1eZ3AoZBc-9Cj5QaPiCwR2G5kn6zu2-zv_9GSyxN6qh9Qam4EZjt_RQVX/edit?usp=sharing) You can copy the contents of this function, choose File > New > Script file, and paste it there. That should enable you to use `FORMATBYTES()` as a function immediately in your Sheets document. This is cleaner than pasting an entire formula. **Downside**: this function is noticeably slower. Google Sheets appears to need about a second to retrieve the script and process its value. This means when your Bytes values update, the cell will show "Loading..." for a second and then finally give the value. It was sufficiently long enough to annoy me to create the above formula. To me, there was also nothing special about the math behind the conversion that mean that Sheets, a program designed for calculations, could not handle. ### Why do we have to do this? Google sorely needs to add KiB, MiB and so on, as actual units available as Number formats. This would mean that a user could type bytes into one cell and format that same cell to receive KiB immediately in that same cell. This absence of functionality is astonishing given the wide range of other scientific units that are available. It also should add these units as part of its `CONVERT()` function. -
seb26 revised this gist
Mar 30, 2019 . 3 changed files with 26 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,3 @@ ``` =ROUND( A2/(1024)^(FLOOR(log(A2)/log(1024))), 2) & " " & SWITCH( FLOOR( log(A2) / log(1024) ) ,0,"Bytes",1,"KiB",2,"MiB",3,"GiB",4,"TiB") ``` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ This formula can be used in Google Sheets to take a value in bytes and then represent it in a human-readable format with binary units (KiB, MiB, GiB, and so on). If your sheet contains data concerning file sizes, systems and other information storage, then representing it in binary units (or IEC units) is absolutely essential and the most correct format for that use case. If you represent your Byte amounts in decimal format (KB, MB, etc), the values are arguably incorrect in the context of these systems. A lot of confusion stems from using the letters 'KB' to represent binary units which should be denominated as 'KiB' instead. See https://en.wikipedia.org/wiki/Binary_prefix#Inconsistent_use_of_units for more background on this topic. ### Example Example table showing differences between the two numbering systems. | Bytes | Decimal | Binary | |-|-|-| | 1024 | 1.02 KB | 1.00 KiB | | 1,048,576 | 1.05 MB | 1.00 MiB | | 4,194,300 | 4.19 MB | 4.00 MiB | | 1,153,156,984 | 1.15 GB | 1.07 GiB | ### Scripts available on the Internet There are a number of scripts, formulas and snippets available on StackOverflow, Google discussion forums and other sites, but the majority of them represent data sizes in decimal instead of binary. #### Decimal units * https://gist.github.com/loganvolkers/933af8513ed8c2268f59c85a31761a43 #### Binary units * https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript/18650828#18650828 – I sourced the math for this formula from this File renamed without changes. -
seb26 renamed this gist
Mar 30, 2019 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
seb26 revised this gist
Mar 30, 2019 . 1 changed file with 14 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ | Bytes | Formula: =A2/(1024)^(FLOOR(log(A2)/log(1024))) |FLOOR(log(A2)/log(1024)|=SWITCH( FLOOR( log(A2) / log(1024) ) ,0,"Bytes",1,"KiB",2,"MiB",3,"GiB",4,"TiB")| All in one | All in one rounded to 2 decimal | |----------------|------------------------------------------------|---|-------|----------------------|---------------------------------| | 512 | 512 | 0 | Bytes | 512 Bytes | 512 Bytes | | 1024 | 1 | 1 | KiB | 1 KiB | 1 KiB | | 2048 | 2 | 1 | KiB | 2 KiB | 2 KiB | | 4096 | 4 | 1 | KiB | 4 KiB | 4 KiB | | 1,048,576 | 1 | 2 | MiB | 1 MiB | 1 MiB | | 36,068,183 | 34.39729977 | 2 | MiB | 34.3972997665405 MiB | 34.4 MiB | | 148,503,136 | 141.6236267 | 2 | MiB | 141.623626708984 MiB | 141.62 MiB | | 1,377,849,741 | 1.283222568 | 3 | GiB | 1.28322256822139 GiB | 1.28 GiB | | 4,242,623,877 | 3.951251392 | 3 | GiB | 3.95125139225274 GiB | 3.95 GiB | | 24,935,584,458 | 23.22307272 | 3 | GiB | 23.2230727169663 GiB | 23.22 GiB | | 27,298,486,746 | 25.42369696 | 3 | GiB | 25.4236969593912 GiB | 25.42 GiB | | 27,298,486,746 | 25.42369696 | 3 | GiB | 25.4236969593912 GiB | 25.42 GiB | -
seb26 revised this gist
Mar 30, 2019 . 1 changed file with 0 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,14 +0,0 @@ -
seb26 created this gist
Mar 30, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ | Bytes | Formula: =A2/(1024)^(FLOOR(log(A2)/log(1024))) |FLOOR(log(A2)/log(1024)|=SWITCH( FLOOR( log(A2) / log(1024) ) ,0,"Bytes",1,"KiB",2,"MiB",3,"GiB",4,"TiB")| All in one | All in one rounded to 2 decimal | |----------------|------------------------------------------------|---|-------|----------------------|---------------------------------| | 512 | 512 | 0 | Bytes | 512 Bytes | 512 Bytes | | 1024 | 1 | 1 | KiB | 1 KiB | 1 KiB | | 2048 | 2 | 1 | KiB | 2 KiB | 2 KiB | | 4096 | 4 | 1 | KiB | 4 KiB | 4 KiB | | 1,048,576 | 1 | 2 | MiB | 1 MiB | 1 MiB | | 36,068,183 | 34.39729977 | 2 | MiB | 34.3972997665405 MiB | 34.4 MiB | | 148,503,136 | 141.6236267 | 2 | MiB | 141.623626708984 MiB | 141.62 MiB | | 1,377,849,741 | 1.283222568 | 3 | GiB | 1.28322256822139 GiB | 1.28 GiB | | 4,242,623,877 | 3.951251392 | 3 | GiB | 3.95125139225274 GiB | 3.95 GiB | | 24,935,584,458 | 23.22307272 | 3 | GiB | 23.2230727169663 GiB | 23.22 GiB | | 27,298,486,746 | 25.42369696 | 3 | GiB | 25.4236969593912 GiB | 25.42 GiB | | 27,298,486,746 | 25.42369696 | 3 | GiB | 25.4236969593912 GiB | 25.42 GiB | This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ ``` =ROUND( A2/(1024)^(FLOOR(log(A2)/log(1024))), 2) & " " & SWITCH( FLOOR( log(A2) / log(1024) ) ,0,"Bytes",1,"KiB",2,"MiB",3,"GiB",4,"TiB") Notes: ^ Specify round decimals here```