-4

Possible Duplicate:
How do I get the size of a file in megabytes using Perl?

I tried some ways to convert MB into TB in perl, but value does not match in perl output and online converter.

whats the best formula to convert the above.

Community
  • 1
  • 1
sun kum
  • 27
  • 1
  • 7
  • 3
    You need to specify whether it's binary or SI prefixes. The size of the difference using binary prefixes (2^20) is ~4.8% larger than that of SI prefixes (1000^2). – Josh Y. Mar 31 '12 at 16:45
  • Perhaps you want to see [How do I get the size of a file in megabytes using Perl?](http://stackoverflow.com/questions/511785/how-do-i-get-the-size-of-a-file-in-megabytes-using-perl) – brian d foy Apr 01 '12 at 00:38

1 Answers1

3

If you don't want any digits after the decimal, you can just shift to the right by 20 bits:

perl -e 'print 202220394 >> 20;'

gives 192. Whereas 202 220 394 megabytes = 192.852396 terabytes.

If you want the decimals, divide by 2^20:

perl -e 'print 202220394 / (1 << 20);'

gives 192.852396011353.

Dave
  • 5,133
  • 21
  • 27
  • Dave,is there any difference in formula for Processor/Virtual Storage vs Disk Storage ? to convert MB into TB ? – sun kum Apr 04 '12 at 16:12
  • Sure. The disk storage people have coopted the "MB" type prefix such that M means 1,000,000 or 10^6. But before that, M meant 2^20 which is slightly more than one million. Some have taken to using "MiB" for "mebibyte" to mean this 2^20 value. Unfortuantely, it's not clear by just looking at a number, but as you hint you can usually assume that disk storage on a package is 10^6 and everything inside a computer is 2^20. TiB is 2^40 vs. 10^12 so you can see the different conversions between MiB/TiB = 2^20/2^40 vs MB/TB = 10^6/10^12. – Dave Apr 05 '12 at 12:38