0

I'm trying to work out a percentage with out using bc as this isn't installed on the clients server and they won't allow it to be.

Normally I would do echo 100/$limit*$usage | bc -l

eg:

echo 100/32212254720*213779238 | bc -l

Which returns a value of .00001271565755207680

This is then rounded using printf "%.0f\n" .66365810111130947790 which returns 0

I'm trying to do this with awk.

awk "BEGIN {print (100/$limit*$usage)}"

eg: awk "BEGIN {print (100/32212254720*4096)}"

Which returns 1.27157e-05 and when rounded using printf "%.0f\n" 1.27157e-05 also returns 0

So both do seem to be working but is awk used correctly and why does it return 1.27157e-05 and not .00001271565755207680?

tripleee
  • 175,061
  • 34
  • 275
  • 318
Tom
  • 1,436
  • 24
  • 50
  • 1
    `printf` is largely the same in `awk`. You just need to make sense of your quoting and variable. I would do: `awk -v limit="$limit" -v usage="$usage" 'BEGIN {printf "%0.0f\n", (100/limit*usage)}'` – dawg Dec 01 '20 at 15:13
  • @dawg thanks I looked into it and came up with the same – Tom Dec 01 '20 at 20:43

0 Answers0