# Use letters to make numbers shorter

### Challenge

Take a number and output it with 4 digits or less using letters and numbers. For example; 270,030 would turn into 270K.

### Key

Billion -> B

Million -> M

Thousand -> K

### Rules

• You may choose to accept input numbers that include commas delimiting every three decimal places (such as 123,456,789).
• Round to nearest, half-up.
• Numbers will only go up to 999,499,999,999 .
• The mantissa part of the answer should be at least 1.
• All letters must be uppercase and as specified above.
• Outputs must be 4 or less digits. (including letters such as B, K, and M)
• Outputs must be as mathematically precise as possible. Example:
• 15,480 -> 20K NOT OK
• 15,480 -> 15.5K GOOD
• If there are multiple outputs with the same mathematical precision, return either one. Example:
• 1000 -> 1000 GOOD
• 1000 -> 1K GOOD

### Examples:

1,234,567 -> 1.23M
999,500 -> 1M
999,499 -> 999K
102 -> 102
1000 -> 1K
1001 -> 1001
100,000 -> 100K
12,345,678,912 -> 12.3B
1,452,815,612 -> 1.45B


# JavaScript, 84 79 bytes

i=>{I=i;for(x=-1;i>=999.5;x++)i/=1e3;return I<1e4?I:+i.toPrecision(3)+'KMB'[x]}


Try it online!

(Takes inputs as numbers)

Special cases the first one where if i < 1000, the number itself is always one of the shortest ways, if not the only shortest way.

Otherwise, it divides it by 1000 until dividing it would make it have no sigificant figures above 1, and chooses a suffix based on how many divisions were done.

• The last case should be 999.5M, or maybe not, unsure based on loose spec. Jul 19, 2017 at 20:20
• Remember that you need to support up to a trillion. Jul 19, 2017 at 20:45
• @GarethPW There is no way to represent numbers above 999,499,999,999 according to the spec (No T -> trillion in the key), so I assumed this was the upper limit. I'll ask, but it's a one byte loss otherwise. Jul 19, 2017 at 20:51

# Python 3, 127 bytes

def f(n):s=str(n);l=len(s)-4;return f"{round(n,~l):,}"[:4].replace(*",.").rstrip('.')+"KMBT"[l//3+(int(s)>4)]if n>9999else n


Try it online!

• a little shorter: def f(n):l=len(str(n))-4;return[n,f"{round(n,~l):,}"[:4].replace(*",.").rstrip('.')+"KMBT"[-~l//3]][n>9999] Jul 19, 2017 at 20:47
• @JonathanAllan Nothing. My mistake. Jul 19, 2017 at 21:23
• @rosslh I did make that alteration but couldn't keep it when fixing the 999499 -> 999M bug. Jul 19, 2017 at 21:27

# JavaScript (ES7), 74 bytes

n=>n<1e4?n:(c=n.toExponential(2).splite).shift()*10**(c%3)+' KMB'[c/3|0]


let f=

n=>n<1e4?n:(c=n.toExponential(2).splite).shift()*10**(c%3)+' KMB'[c/3|0]

console.log(f(1234567)); // 1.23M
console.log(f(999500)); // 1M
console.log(f(999499)); // 999K
console.log(f(102)); // 102
console.log(f(1000)); // 1000
console.log(f(1001)); // 1001
console.log(f(100000)); // 100K
console.log(f(12345678912)); // 12.3B
console.log(f(1452815612)); // 1.45B

# JavaScript (ES6), 80 bytes

n=>n<1e4?n:(c=n.toExponential(2).splite).shift()*[1,10,100][c%3]+' KMB'[c/3|0]


let f=

n=>n<1e4?n:(c=n.toExponential(2).splite).shift()*[1,10,100][c%3]+' KMB'[c/3|0]

console.log(f(1234567)); // 1.23M
console.log(f(999500)); // 1M
console.log(f(999499)); // 999K
console.log(f(102)); // 102
console.log(f(1000)); // 1000
console.log(f(1001)); // 1001
console.log(f(100000)); // 100K
console.log(f(12345678912)); // 12.3B
console.log(f(1452815612)); // 1.45B