Via linux - Is there a command line calculator for byte calculations? - Stack Overflow, I found about GNU Units - though without examples on the SO page; and as I didn't see it listed here, here is a small note about it.
First, check if the units are present:
$ units --check-verbose |grep byte
doing 'byte'
$ units --check-verbose |grep mega
doing 'megalerg'
doing 'mega'
$ units --check-verbose |grep mebi
doing 'mebi'
Given that they are, do a conversion - printf
format specifiers are accepted to format the numeric result:
$ units --one-line -o "%.15g" '20023450 bytes' 'megabytes' # also --terse
* 20.02345
$ units --one-line -o "%.15g" '20023450 bytes' 'mebibytes'
* 19.0958499908447
$ units --one-line -o "%.5g" '20023450 bytes' 'mebibytes'
* 19.096