123456789101112131415161718192021222324252627(* CR-someday amokhov: Add KiB, MiB, GiB. *)letconversion_table=[(["B";"bytes"],1L);(["kB";"KB";"kilobytes"],1_000L);(["MB";"megabytes"],1_000_000L);(["GB";"gigabytes"],1_000_000_000L);(["TB";"terabytes"],1_000_000_000_000L)]letppx=(* We go through the list to find the first unit that is greater than the
number of bytes and take the predecessor as the units for printing. For the
special base case where no conversion is necessary we don't print as a
float. *)letsuffix,value=letrecloop=function|[]->assertfalse|[(units,value)]->(List.hdunits,value)|(units,value)::((_,value')::_asl)->ifx=0Lthen(List.hdunits,value)elseifvalue<=x&&x<value'then(List.hdunits,value)elselooplinloop@@conversion_tableinifvalue=1LthenPrintf.sprintf"%Ld%s"xsuffixelsePrintf.sprintf"%.2f%s"(Int64.to_floatx/.Int64.to_floatvalue)suffix