My goto formula for this after many years of refinement is the here.
You can use the beginning variable definitions to define aspects of the conversion:
- cell defines the cell containing the bytes to format
- cell_style defines if you're using the R1C1 (rowcolumn) or the A1 (alphanumeric) cell reference style
- unit_type defines if you want the resulting output calculated to a binary or metric value
=LET(
README_,"This formula formats a byte count, rounding it to its significant digit and to two decimal places, applying the appropriate unit of measurement",
README1,"Adjust the below [variable] definitions to your need, where the viable options are (bracketed)",
README2,"[cell] specify the individual cell address containing the byte count to format, this should be contained within double quotation marks",
README3,"[cell_style] define if you're using the R1C1 (rowcolumn) or the A1 (alphanumeric) cell reference style",
README4,"[unit_type] define if you want the resulting output calculated to a base 2 (binary) or base 10 (metric) value",
CONSTANTS,"Do not alter these",
binary,1024,
metric,1000,
rowcolumn,0,
alphanumeric,1,
VARIABLES,"Adjust these to your need",
cell,"RC[-1]",
unit_type,binary,
cell_style,rowcolumn,
IF(N("Error if the speficied cell is not a number")+
NOT(ISNUMBER(INDIRECT(cell,cell_style))),"#MISSINGNO",
IF(unit_type=1000
+N("Calculate the result at base 10"),
IF((INDIRECT(cell,cell_style)>=1000^8),
TEXT((INDIRECT(cell,cell_style)/1000/1000/1000/1000/1000/1000/1000/1000),"#,## ????.00 \Y\B"),
IF((INDIRECT(cell,cell_style)>=1000^7),
TEXT((INDIRECT(cell,cell_style)/1000/1000/1000/1000/1000/1000/1000),"#,## ????.00 \Z\B"),
IF((INDIRECT(cell,cell_style)>=1000^6),
TEXT((INDIRECT(cell,cell_style)/1000/1000/1000/1000/1000/1000),"#,## ????.00 \E\B"),
IF((INDIRECT(cell,cell_style)>=1000^5),
TEXT((INDIRECT(cell,cell_style)/1000/1000/1000/1000/1000),"#,## ????.00 \P\B"),
IF((INDIRECT(cell,cell_style)>=1000^4),
TEXT((INDIRECT(cell,cell_style)/1000/1000/1000/1000),"#,## ????.00 \T\B"),
IF((INDIRECT(cell,cell_style)>=1000^3),
TEXT((INDIRECT(cell,cell_style)/1000/1000/1000),"#,## ????.00 \G\B"),
IF((INDIRECT(cell,cell_style)>=1000^2),
TEXT((INDIRECT(cell,cell_style)/1000/1000),"#,## ????.00 \M\B"),
IF((INDIRECT(cell,cell_style)>=1000^1),
TEXT((INDIRECT(cell,cell_style)/1000),"#,## ????.00 \K\B"),
IF(INDIRECT(cell,cell_style)=1,
"1 B",TEXT(INDIRECT(cell,cell_style),"## ????0 \B")
))))))))),
IF(unit_type=1024
+N("Calculate the result at base 2"),
IF((INDIRECT(cell,cell_style)>=POWER(2,80)),
TEXT((INDIRECT(cell,cell_style)/POWER(2,80)),"#,## ????.00 \Y\i\B"),
IF((INDIRECT(cell,cell_style)>=POWER(2,70)),
TEXT((INDIRECT(cell,cell_style)/POWER(2,70)),"#,## ????.00 \Z\i\B"),
IF((INDIRECT(cell,cell_style)>=POWER(2,60)),
TEXT((INDIRECT(cell,cell_style)/POWER(2,60)),"#,## ????.00 \E\i\B"),
IF((INDIRECT(cell,cell_style)>=POWER(2,50)),
TEXT((INDIRECT(cell,cell_style)/POWER(2,50)),"#,## ????.00 \P\i\B"),
IF((INDIRECT(cell,cell_style)>=POWER(2,40)),
TEXT((INDIRECT(cell,cell_style)/POWER(2,40)),"#,## ????.00 \T\i\B"),
IF((INDIRECT(cell,cell_style)>=POWER(2,30)),
TEXT((INDIRECT(cell,cell_style)/POWER(2,30)),"#,## ????.00 \G\i\B"),
IF((INDIRECT(cell,cell_style)>=POWER(2,20)),
TEXT((INDIRECT(cell,cell_style)/POWER(2,20)),"#,## ????.00 \M\i\B"),
IF((INDIRECT(cell,cell_style)>=1024),
TEXT((INDIRECT(cell,cell_style)/1024),"#,## ????.00 \K\i\B"),
IF(INDIRECT(cell,cell_style)=1,
"1 B",TEXT(INDIRECT(cell,cell_style),"## ????0 \B")
)))))))))))))
If you ever want to do the reverse, and derive a rough raw count of bytes from a pre-formatted value, I've got a formula for that too.
=IF(A1>POWER(1024,4),TRUNC(A1/POWER(1024,4),2)&" TB", IF(A1>POWER(1024,3),TRUNC(A1/POWER(1024,3),2)&" GB", IF(A1>POWER(1024,2), ROUND(A1/POWER(1024,2),0)&" MB", ROUND(A1/1024,0)&" KB")))
=IF(A1>POWER(1024;4);TRUNC(A1/POWER(1024;4);2)&" TB"; IF(A1>POWER(1024;3);TRUNC(A1/POWER(1024;3);2)&" GB"; IF(A1>POWER(1024;2); ROUND(A1/POWER(1024;2);0)&" MB"; ROUND(A1/1024;0)&" KB")))