Skip to main content
added 77 characters in body
Source Link
GammaFunction
  • 6.9k
  • 13
  • 23

Bash, 83 bytes

a=('' k M G T)
s=
(($1>99))&&s=${1: -3}
echo `$0 ${1:0:-3} $[$2+1]`${s:-$1}${a[$2]}

Try it online! (link shows 82 bytes, but uses function recursion instead of $0-recursion)

Nice little recursive solution.

Bash doesn't like invalid string indexing, which both helps (gives us our exit condition for free) and hurts (we have to have separate logic for when our number has n != 0 mod 3 digits).

Bash, 83 bytes

a=('' k M G T)
s=
(($1>99))&&s=${1: -3}
echo `$0 ${1:0:-3} $[$2+1]`${s:-$1}${a[$2]}

Try it online!

Nice little recursive solution.

Bash doesn't like invalid string indexing, which both helps (gives us our exit condition for free) and hurts (we have to have separate logic for when our number has n != 0 mod 3 digits).

Bash, 83 bytes

a=('' k M G T)
s=
(($1>99))&&s=${1: -3}
echo `$0 ${1:0:-3} $[$2+1]`${s:-$1}${a[$2]}

Try it online! (link shows 82 bytes, but uses function recursion instead of $0-recursion)

Nice little recursive solution.

Bash doesn't like invalid string indexing, which both helps (gives us our exit condition for free) and hurts (we have to have separate logic for when our number has n != 0 mod 3 digits).

Source Link
GammaFunction
  • 6.9k
  • 13
  • 23

Bash, 83 bytes

a=('' k M G T)
s=
(($1>99))&&s=${1: -3}
echo `$0 ${1:0:-3} $[$2+1]`${s:-$1}${a[$2]}

Try it online!

Nice little recursive solution.

Bash doesn't like invalid string indexing, which both helps (gives us our exit condition for free) and hurts (we have to have separate logic for when our number has n != 0 mod 3 digits).