Skip to main content
2 of 8
Indicating full program to differentiate from another C submission.
user avatar
user avatar

#C, 189 bytes (full program)

typedef double D;D atof(char*);D log10(D);main(int i,char**v){D x=atof(*++v);if(x<1e3)printf(*v);else{while(x>=1e3){++i;x/=1024;}printf("%.*f%ci",2-(int)log10(x),x,"???KMG"[i]);}puts("B");}

Save as doit.c and compile:

> gcc -o doit doit.c -lm

Run

> ./doit 1000
0.98KiB

Test cases:

> for i in 0 1 42 999 1000 1024 2018 10086 100010 456789 20080705 954437177 1084587701 1207959551 2147483647 ; do ./doit $i ; done
0B
1B
42B
999B
0.98KiB
1.00KiB
1.97KiB
9.85KiB
97.7KiB
446KiB
19.2MiB
910MiB
1.01GiB
1.12GiB
2.00GiB
user15259