Skip to content

Instantly share code, notes, and snippets.

@peterfpeterson
Created April 7, 2014 18:47
Show Gist options
  • Save peterfpeterson/10029126 to your computer and use it in GitHub Desktop.
Save peterfpeterson/10029126 to your computer and use it in GitHub Desktop.
# Simple calculator - from https://github.com/mathiasbynens/dotfiles/blob/master/.functions
function calc() {
local result=""
result="$(printf "scale=10;$*\n" | bc --mathlib | tr -d '\\\n')"
# └─ default (when `--mathlib` is used) is 20
#
if [[ "$result" == *.* ]]; then
# improve the output for decimal numbers
printf "$result" |
sed -e 's/^\./0./' `# add "0" for cases like ".5"` \
-e 's/^-\./-0./' `# add "0" for cases like "-.5"`\
-e 's/0*$//;s/\.$//' # remove trailing zeros
else
printf "$result"
fi
printf "\n"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment