TiB, not TB, Seriously?

-1

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

Matthew Roh

Posted 2017-03-25T03:49:24.593

Reputation: 5 043

2This needs more specification. What units do we have to handle? What number ranges? With what precision? – xnor – 2017-03-25T03:53:52.347

1@xnor precision is specified(rounded half-up to integer value), everything else is edited to specify – Matthew Roh – 2017-03-25T03:57:34.067

2Better, 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. – xnor – 2017-03-25T04:01:13.583

2Rounding an precision are different. You use fractional terabytes but entire megabytes in your examples. – Dennis – 2017-03-25T04:01:51.770

@Dennis Huh? I don't understand what you mean. – Matthew Roh – 2017-03-25T04:03:53.827

1Why is the first output 711 MB and not 710.9 MB? – Dennis – 2017-03-25T04:06:12.137

@Dennis specified: rounded half-up to integer value – Matthew Roh – 2017-03-25T04:06:58.787

So the output for 1.4 TiB would be 2 TB? – Dennis – 2017-03-25T04:08:32.547

@Dennis Yup. 1.4 TiB=> 1.5...TB => 2TB. – Matthew Roh – 2017-03-25T04:10:02.287

What would the output for 999 MiB be? – Dennis – 2017-03-25T04:18:44.497

@Dennis 1048 it is. – Matthew Roh – 2017-03-25T05:47:09.227

6TiB or not TiB, that is the question. – Neil – 2017-03-25T10:26:56.670

@SIGSEGV So we don't have to "jump" prefixes? You should clarify that in the challenge spec. – Dennis – 2017-03-25T19:28:38.373

10Calling 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. – Peter Taylor – 2017-03-25T20:24:35.257

1Yeah, I don't know anyone who actually calls 1024 bytes a "kibibyte". You read it Kilobytes. The existence of a decimal form/prefix at all is the real problem – mbomb007 – 2017-03-27T16:08:37.447

Answers

1

Python 3.6, 78 77 bytes

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

Ben Frankel

Posted 2017-03-25T03:49:24.593

Reputation: 301

1

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!

KarlKastor

Posted 2017-03-25T03:49:24.593

Reputation: 2 352

1

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

Adám

Posted 2017-03-25T03:49:24.593

Reputation: 37 779

It should be 1.024 not 1.044 in your explanation – fəˈnɛtɪk – 2017-03-27T11:55:32.393

@fəˈnɛtɪk Indeed. Well spotted. – Adám – 2017-03-27T12:29:37.913

1

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>.

Titus

Posted 2017-03-25T03:49:24.593

Reputation: 13 814

0

Mathematica, 44 43 bytes

It has a built-in function for everything

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

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

kennytm

Posted 2017-03-25T03:49:24.593

Reputation: 6 847

0

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>

Neil

Posted 2017-03-25T03:49:24.593

Reputation: 95 035