Skip to content

Instantly share code, notes, and snippets.

@theapache64
Created June 26, 2025 07:28
Show Gist options
  • Save theapache64/1f5b5d1d1c5bb20f9ed98235fd2de6d9 to your computer and use it in GitHub Desktop.
Save theapache64/1f5b5d1d1c5bb20f9ed98235fd2de6d9 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
diff=$(echo "scale=4 ; $1 - $2" | bc)
diffPerc=$(echo "scale=4 ; $diff / $2 * 100" | bc)
printf "Difference of %s and %s are %.2f%%\n" "$1" "$2" "$diffPerc"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment