Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Created February 17, 2014 22:41
Show Gist options
  • Save coderofsalvation/9060712 to your computer and use it in GitHub Desktop.
Save coderofsalvation/9060712 to your computer and use it in GitHub Desktop.
simple calculation function for integer *and* floating point math
# simple calculation function for integer *and* floating point math
#
# @param string to be calculated
# @param precision (default = 0)
# usage: calculate "(1+1)/2/10" <- outputs 0
# calculate "(1+1)/2/10" 10 <- outputs 0.1
# echo $(calculate "1+1") <- outputs 2
calculate(){
if [[ -n "$2" ]]; then
which bc &>/dev/null && { echo "scale=$2;$1" | bc; } || echo "bc is not installed";
else echo $(($1)); fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment