Skip to main content
Commonmark migration
Source Link

#C, 154 165 191 189 bytes (full program)

C, 154 165 191 189 bytes (full program)

Big thanks to O.O.Balance and l4m2 for suggestions and providing this TIO and this TIO!

double atof(),x;main(i,v)char**v;{x=atof(*++v);if(x<1e3)printf("%sB",*v);else{for(;x>=999.5;++i)x/=1024;printf("%.*f%ciB",2-(int)log10(x),x,"KMG"[i-3]);}}

Save as doit.c, compile using gcc -o doit doit.c -lm, and run:

> ./doit 1000
0.98KiB

Test cases:

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

#C, 154 165 191 189 bytes (full program)

Big thanks to O.O.Balance and l4m2 for suggestions and providing this TIO and this TIO!

double atof(),x;main(i,v)char**v;{x=atof(*++v);if(x<1e3)printf("%sB",*v);else{for(;x>=999.5;++i)x/=1024;printf("%.*f%ciB",2-(int)log10(x),x,"KMG"[i-3]);}}

Save as doit.c, compile using gcc -o doit doit.c -lm, and run:

> ./doit 1000
0.98KiB

Test cases:

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

C, 154 165 191 189 bytes (full program)

Big thanks to O.O.Balance and l4m2 for suggestions and providing this TIO and this TIO!

double atof(),x;main(i,v)char**v;{x=atof(*++v);if(x<1e3)printf("%sB",*v);else{for(;x>=999.5;++i)x/=1024;printf("%.*f%ciB",2-(int)log10(x),x,"KMG"[i-3]);}}

Save as doit.c, compile using gcc -o doit doit.c -lm, and run:

> ./doit 1000
0.98KiB

Test cases:

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

#C, 165154 165 191 189 bytes (full program)

Big thanks to O.O.Balance and l4m2 for suggestions and providing athis TIO. and this TIO!

double atof(char*);main,x;main(i,v)char**v;{double x=atof(*++v);if(x<1e3)printf("%sB",*v);else{for(;x>=999.5;++i)x/=1024;printf("%.*f%ciB",2-(int)log10(x),x,"???KMG"[i]"KMG"[i-3]);}}

Save as doit.c and, compile using gcc -o doit doit.c -lm, and run:

> ./doit 1000
0.98KiB

Test cases:

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

#C, 165 191 189 bytes (full program)

Big thanks to O.O.Balance for suggestions and providing a TIO.

double atof(char*);main(i,v)char**v;{double x=atof(*++v);if(x<1e3)printf("%sB",*v);else{for(;x>=999.5;++i)x/=1024;printf("%.*f%ciB",2-(int)log10(x),x,"???KMG"[i]);}}

Save as doit.c and compile using gcc -o doit doit.c -lm, and run:

> ./doit 1000
0.98KiB

Test cases:

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

#C, 154 165 191 189 bytes (full program)

Big thanks to O.O.Balance and l4m2 for suggestions and providing this TIO and this TIO!

double atof(),x;main(i,v)char**v;{x=atof(*++v);if(x<1e3)printf("%sB",*v);else{for(;x>=999.5;++i)x/=1024;printf("%.*f%ciB",2-(int)log10(x),x,"KMG"[i-3]);}}

Save as doit.c, compile using gcc -o doit doit.c -lm, and run:

> ./doit 1000
0.98KiB

Test cases:

> for i in 0 1 42 999 1000 1024 2018 10086 100010 456789 1023079 1023488 20080705 954437177 1084587701 1207959551 2147483647 ; do ./doit $i ; echo; done
0B
1B
42B
999B
0.98KiB
1.00KiB
1.97KiB
9.85KiB
97.7KiB
446KiB
999KiB
0.98MiB
19.2MiB
910MiB
1.01GiB
1.12GiB
2.00GiB
Implementing OO.Balance's changes.
Source Link
user15259
user15259

#C, 191165 191 189 bytes (full program)

Big thanks to O.O.Balance for suggestions and providing a TIO.

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

Save as doit.c and compile using gcc -o doit doit.c -lm, and run:

> ./doit 1000
0.98KiB

Test cases:

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

#C, 191 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>=999.5){++i;x/=1024;}printf("%.*f%ci",2-(int)log10(x),x,"???KMG"[i]);}puts("B");}

Save as doit.c and compile using gcc -o doit doit.c -lm, and run:

> ./doit 1000
0.98KiB

Test cases:

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

#C, 165 191 189 bytes (full program)

Big thanks to O.O.Balance for suggestions and providing a TIO.

double atof(char*);main(i,v)char**v;{double x=atof(*++v);if(x<1e3)printf("%sB",*v);else{for(;x>=999.5;++i)x/=1024;printf("%.*f%ciB",2-(int)log10(x),x,"???KMG"[i]);}}

Save as doit.c and compile using gcc -o doit doit.c -lm, and run:

> ./doit 1000
0.98KiB

Test cases:

> for i in 0 1 42 999 1000 1024 2018 10086 100010 456789 1023079 1023488 20080705 954437177 1084587701 1207959551 2147483647 ; do ./doit $i ; echo; done
0B
1B
42B
999B
0.98KiB
1.00KiB
1.97KiB
9.85KiB
97.7KiB
446KiB
999KiB
0.98MiB
19.2MiB
910MiB
1.01GiB
1.12GiB
2.00GiB
To account for `1023488`.
Source Link
user15259
user15259
Loading
To account for `1023488`.
Source Link
user15259
user15259
Loading
Condensing format.
Source Link
user15259
user15259
Loading
Indicating full program to differentiate from another C submission.
Source Link
user15259
user15259
Loading
Source Link
user15259
user15259
Loading