Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env lua
- local prefixes = {
- [0] = "",
- [3] = "K",
- [6] = "M",
- [9] = "G",
- [12] = "T",
- [15] = "P",
- };
- function round(n)
- return tonumber(string.format("%.1f", n));
- end
- function divideNumber(n)
- local n = tonumber(n);
- if not n then error("Not a number", 2) end
- -- Get the log
- local l = ((n == 0) and 0) or math.floor(math.log(n, 10));
- -- Make it a multiple of 3, rounding down
- l = l - (l % 3);
- return round(n / math.pow(10, l))..prefixes[l];
- end
- for input in io.lines() do
- io.write(string.gsub(input, "%d+", divideNumber).."\n");
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement