Visualize a large int 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
JavaScript (ES6), 64 bytes

Takes input as a string.



f=(n,i=0,[d,t]=n.split(/(...)$/))=&gt;t?f(d,i+1)+['kMGT'[d&amp;&amp;i]]+t:d


Try it online!

How?

The regular expression /(...)/$ matches the last three digits of the integer, or nothing at all if there are less than 3 digits remaining.

Because there's a capturing group, these digits -- if present -- are included in the output of the .split() method.

Examples:

'123456789'.split(/(...)$/) // --&gt; [ '123456', '789' '' ]
'123'.split(/(...)$/))      // --&gt; [ '', '123' ]
'12'.split(/(...)$/))       // --&gt; [ '12' ]
''.split(/(...)$/))         // --&gt; [ '' ]


Commented

f = (                 // f is a recursive function taking:
  n,                  //   n = input
  i = 0,              //   i = index of next prefix
  [d, t] =            //   d = either the leading digits without the last 3 ones,
                      //       or all remaining digits if there are less than 3
    n.split(/(...)$/) //   t = last three digits (or undefined)
) =&gt;                  //
  t ?                 // if t is defined:
    f(d, i + 1) +     //   append the result of a recursive call with d and i + 1
    ['kMGT'[d &amp;&amp; i]]  //   append the prefix if d is defined, or an empty string otherwise
    + t               //   append the last 3 digits
  :                   // else:
    d                 //   stop recursion and return the remaining digits