Skip to content

Instantly share code, notes, and snippets.

@theapache64
Created June 26, 2025 07:31
Show Gist options
  • Save theapache64/bad0f9bd9407dc47241e8d32646ec9e8 to your computer and use it in GitHub Desktop.
Save theapache64/bad0f9bd9407dc47241e8d32646ec9e8 to your computer and use it in GitHub Desktop.
pw() {
result=$(echo "scale=4 ; $1 / $2 * 100" | bc)
printf "%s is %.2f%% of %s\n" "$1" "$result" "$2"
if (( $(echo "$1 < $2" | bc -l) ))
then
diff=$(echo "scale=4 ; $2 - $1" | bc)
diffPerc=$(echo "scale=4 ; $diff / $2 * 100" | bc)
printf "%s is a %.2f%% decrease of %s\n" "$1" "$diffPerc" "$2"
else
diff=$(echo "scale=4 ; $1 - $2" | bc)
diffPerc=$(echo "scale=4 ; $diff / $2 * 100" | bc)
printf "%s is a %.2f%% increase of %s\n" "$1" "$diffPerc" "$2"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment