1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
let foo n =
  let u = " kMGT" in
  let l = String.length n in
  let idx = if (l mod 3) == 0 then (l / 3) - 1 else (l / 3) in
    Printf.printf "%s%s %cbytes\n"
      (String.sub n 0 (l - (idx * 3)))
      (if l > 3 then "." ^ (String.sub n (l-(idx*3)) 1) else "")
      u.[idx]
;;

(*
短く
let f n = let u = " kMGT" in let l = String.length n in let x = if (l mod 3) == 0 then (l / 3) - 1 else (l / 3) in Printf.printf "%s%s %cbytes\n" (String.sub n 0 (l - (x * 3))) (if l > 3 then "."^(String.sub n (l-(x*3)) 1) else "") u.[x];;
*)

foo "76543210";;