Skip to content

Instantly share code, notes, and snippets.

@84adam
Created March 22, 2017 23:05
Show Gist options
  • Save 84adam/c08008900bb4f7f0d51ad6bfc663f75b to your computer and use it in GitHub Desktop.
Save 84adam/c08008900bb4f7f0d51ad6bfc663f75b to your computer and use it in GitHub Desktop.
Calculate rounded percentage in bash using bc
CODE:
x=<FIRSTNUM>; y=<SECONDNUM>; printf $(echo "scale=4; $x/$y * 100" | bc | cut -d . -f 1)%%
EXAMPLE:
x=55; y=85; printf $(echo "scale=4; $x/$y * 100" | bc | cut -d . -f 1)%%
OUTPUT:
64%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment