-1
\$\begingroup\$

If you're like me (and/or use Windows), You should have found this issue quite disturbing.

You plug in a hard drive (2TB for my occasion), and realize it doesn't show as 2TB, but a bit smaller size which is clearly not 2TB.

Over some research, I found it was because of Microsoft's mistake. Mistyping Binary prefix as Decimal prefixes. (i.e. They typed 1TiB as 1TB)

So, I would like to see a Binary prefix to Decimal prefix program. Since this is a big problem (and some computers are literally toasters), You have to make your program as short as possible.

conversion chart I found


I/O

You will be given the size (positive real number, will not exceed 2ˆ16 when converted), and the unit (4 of them, KiB, MiB, GiB and TiB, each corresponds to 1024ˆ1, 1024ˆ2, 1024ˆ3 and 1024ˆ4 bytes)

You have to output the (decimal representation of) the size (rounded half-up to integer value), and the corresponding unit.


Examples

678 MiB => 711 MB
1.8 TiB => 2 TB

Tip: Google has a unit converter

\$\endgroup\$
  • 2
    \$\begingroup\$ This needs more specification. What units do we have to handle? What number ranges? With what precision? \$\endgroup\$ – xnor Mar 25 '17 at 3:53
  • 2
    \$\begingroup\$ Better, but there's more to specify. How much precision can the inputs be given with? Can they be negative? Can an input be given like .15? 1.00? What about outputs? There's a bunch of things like this to pin down. I'd suggest moving it to the sandbox. \$\endgroup\$ – xnor Mar 25 '17 at 4:01
  • 2
    \$\begingroup\$ Rounding an precision are different. You use fractional terabytes but entire megabytes in your examples. \$\endgroup\$ – Dennis Mar 25 '17 at 4:01
  • 6
    \$\begingroup\$ TiB or not TiB, that is the question. \$\endgroup\$ – Neil Mar 25 '17 at 10:26
  • 10
    \$\begingroup\$ Calling the discrepancy "Microsoft's horrible mistake" is somewhat flamebait. All they're doing is maintaining backwards compatibility with what us old-timers (read: anyone in their 30s or up) grew up knowing as the standard. \$\endgroup\$ – Peter Taylor Mar 25 '17 at 20:24
1
\$\begingroup\$

Python 3.6, 78 77 bytes

a,b=input().split()
print(f'{eval(a)*1.024**" KMGT".find(b[0]):.0f} {b[0]}B')
\$\endgroup\$
1
\$\begingroup\$

Pyth, 27 25 35 Bytes

=H@z_3s[.E*v<4z^1.024x" KMGT"HdH\B

Adding the unit to the output costed me quite some bytes.

Try it!

\$\endgroup\$
1
\$\begingroup\$

PHP, 63 bytes

<?=1.024**strpos(_KMGT,($m=$argv[2][0]))*$argv[1]+.5|0," $m",B;

takes input from command line arguments;
run with echo '<code>' | php -n or save to file and run with php -n <filename>.

\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog), 26 bytes

Prompts for unit and size, and outputs unit and size.

⌊.5+⎕×1.024*'KMGT'⍳⊃⎕←⍞∩⎕A

Try it online!

∩⎕A intersection with uppercase Alphabet of

 character input (unit) from STDIN

⎕← output that to STDOUT

 get the first character of the outputted value

'KMGT'⍳ find the 1-based index of that in this string

1.024* raise this number that that power

⎕× multiply numeric input from STDIN with that

.5+ add a half to that

 round that down

\$\endgroup\$
  • \$\begingroup\$ It should be 1.024 not 1.044 in your explanation \$\endgroup\$ – fəˈnɛtɪk Mar 27 '17 at 11:55
  • \$\begingroup\$ @fəˈnɛtɪk Indeed. Well spotted. \$\endgroup\$ – Adám Mar 27 '17 at 12:29
0
\$\begingroup\$

JavaScript (ES6), 71 bytes

f=
(s,[n,[u]]=s.split` `)=>(n*1.024**` KMGT`.search(u)).toFixed()+` ${u}B`
<input oninput=o.textContent=/\s[KMGT]/.test(this.value)?f(this.value):``><span id=o>

\$\endgroup\$
0
\$\begingroup\$

Mathematica, 44 43 bytes

It has a built-in function for everything 😐

Round@UnitConvert[#,StringPart[#,-3]<>"B"]&

(Input should be a string "678 MiB".)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.