EnableExplicit

Procedure.s SizeToXiBStringEx(xDbl1.q, xDblCmp.q, NumDecimals.i, ForceDecimals.i, OverFlowShift.i, *ptrExtStr)
  Define.q xDbl2

  If xDblCmp < ($400 << OverFlowShift) And Not ForceDecimals
    If *ptrExtStr
      PokeS(*ptrExtStr, "B", -1, #PB_Ascii)
      EndIf
    ProcedureReturn Str(xDbl1)
    EndIf
  If xDblCmp < ($100000 << OverFlowShift)
    If *ptrExtStr
      PokeS(*ptrExtStr, "KiB", -1, #PB_Ascii)
      EndIf
    ProcedureReturn StrF(xDbl1 / 1024, NumDecimals)
    EndIf
  ;OverFlowShift = 1 << OverFlowShift
  xDbl2 = $40000000
  xDbl2 << OverFlowShift
  If xDblCmp < xDbl2
    xDbl1 = xDbl1 >> 10
    If *ptrExtStr
      PokeS(*ptrExtStr, "MiB", -1, #PB_Ascii)
      EndIf
    ProcedureReturn StrF(xDbl1 / 1024, NumDecimals)
    EndIf
  xDbl2 = $10000000000
  xDbl2 << OverFlowShift
  If xDblCmp < xDbl2
    xDbl1 = xDbl1 >> 20
    If *ptrExtStr
      PokeS(*ptrExtStr, "GiB", -1, #PB_Ascii)
      EndIf
    ProcedureReturn StrF(xDbl1 / 1024, NumDecimals)
    EndIf
  xDbl2 = $4000000000000
  xDbl2 << OverFlowShift
  If xDblCmp < xDbl2
    xDbl1 = xDbl1 >> 30
    If *ptrExtStr
      PokeS(*ptrExtStr, "TiB", -1, #PB_Ascii)
      EndIf
    ProcedureReturn StrF(xDbl1 / 1024, NumDecimals)
    EndIf
  xDbl2 = $1000000000000000
  xDbl2 << OverFlowShift
  If xDblCmp < xDbl2
    xDbl1 = xDbl1 >> 40
    If *ptrExtStr
      PokeS(*ptrExtStr, "PiB", -1, #PB_Ascii)
      EndIf
    ProcedureReturn StrF(xDbl1 / 1024, NumDecimals)
    EndIf
  xDbl1 = xDbl1 >> 50
  If *ptrExtStr
    PokeS(*ptrExtStr, "EiB", -1, #PB_Ascii)
    EndIf
  ProcedureReturn StrF(xDbl1 / 1024, NumDecimals)
  EndProcedure
; IDE Options = PureBasic 5.30 (Windows - x86)
; CursorPosition = 59
; FirstLine = 29
; Folding = -
; EnableUnicode
; EnableXP