19
3
Convert a large int (e. g. 1234567890) into a string containing the usual decimal prefixes k, M, G, T, etc. for kilo, mega, giga, tera, etc. The result shall be groups of three digits, interspersed by the prefixes (e. g. 1G234M567k890
), so that it is easy to glance the order of magnitude of the number without counting the digits.
It shall work for numbers of all digit counts up to 15. Only non-negative numbers are allowed as input.
More test cases:
0 → 0
123 → 123
1234 → 1k234
1000 → 1k000
1000000000 → 1G000M000k000
@Arnauld Look at my example result. It contains several prefixes, but if the number is small, it could also contain one or even none (for numbers below 100000 and below 1000). – Alfe – 2019-10-11T10:45:48.503
Can I take the input as a string, since my language supports integers only up to 2^32? – Galen Ivanov – 2019-10-11T10:53:45.140
1you don't even need peta, because 15 digits is only up to tera. 123T 456G 789M 012k 345 – Dorian – 2019-10-11T10:54:03.123
2@GalenIvanov If the language cannot handle integers up to 15 digits, the input can be a string representation of the number in decimal format. – Alfe – 2019-10-11T11:00:50.390
4So a block of 0s should still display? E.g.,
1000234 -> 1M000k234
– Veskah – 2019-10-11T14:29:54.5704can we get more than 1 test case? – aaaaa says reinstate Monica – 2019-10-11T19:07:08.617
The number shall be displayed completely, just interspersed with the correct prefixes to ease orientation. I will add more test cases. – Alfe – 2019-10-11T23:19:58.097
4"easy to glance the order of magnitude". I personally find
1G000M000k000
much harder to parse than1 000 000 000
– Eric Duminil – 2019-10-13T19:16:27.940@EricDuminil I agree. That's why in practice I would use
ᵏᴹᴳᵀ
as markers instead. I find a1ᵀ002ᴳ289ᴹ732ᵏ929
way better to read than the same with ASCII letters. And if the colour for the letters also is set to something unobtrusive (grey, for instance), this effect is even improved. This fancy Unicode thing brought me some criticism in my own answer, though (Python3 below). – Alfe – 2019-10-14T08:39:50.133