TehOne
1
The source number is always in megabytes and I need a script to covert into either MB,GB or TB displayed in a specific format.
Source number examples:
5345376635
34255
5453645846353
The result has to be maximum 2 numbers after the comma
1.13 TB
134.17 TB
413.46 GB
678.45 MB
I know a couple ways to calculate it but how to make it just 2 numbers after comma?
EXAMPLE1="`expr $SOURCE / 1024 / 1024` TB"
EXAMPLE2="$(($SOURCE/1024/1024)) TB"
EXAMPLE3="`echo|awk '{print '$SOURCE' / 1024 / 1024}'` TB"
Use printf to format the output:
printf "%.2f\n" $SOURCE
Regards
TehOne
3
Thanks that does solve my main problem but one more question..
TEST="`echo|awk '{print 7068099 / 1024 / 1024}' | printf "%.2f\n"` TB"
echo $TEST
How can I make it work like this?
I've got the following alternative solution but that's one unnecessary step!?
TEST=`echo|awk '{print 7068099 / 1024 / 1024}'
TEST2="`printf "%.2f\n" $TEST` TB"
echo $TEST2
Should be something like:
TEST=$(awk 'BEGIN{printf "%.2f\n", 7068099 / 1024 / 1024}')
Or as a variable:
TEST=$(awk -v a="7068099" 'BEGIN{printf "%.2f\n", a/1024/1024}')
Regards