Skip to main content
5 of 5
Commonmark migration

CJam, 35 27 bytes

ri{_e-3_i}g;1mOo]," kMG"='B

Thanks Dennis for removing 8 bytes.

This doesn't print .0 in the online interpreter. But as Dennis has pointed out, it works fine in the Java interpreter.

Explanation

ri         e# Read the input as an integer.
{          e# Do:
    _e-3   e#   Make a copy and divide by 1000.
           e#   This will generate one more item in the stack for each iteration.
    _i     e#   Make a copy and truncate to integer.
}g         e# until the integer part is 0.
;          e# Discard the final value with integer part 0.
1mOo       e# Output the number before it with the correct format.
],         e# Count the number of iterations - 1.
" kMG"=    e# Select a character according to the number of iterations.
'B         e# Output B.
jimmy23013
  • 37k
  • 6
  • 77
  • 148