diff --git a/.zshrc b/.zshrc index fb861c6..c4b6e89 100644 --- a/.zshrc +++ b/.zshrc @@ -173,3 +173,20 @@ alias gcf="git commit --fixup=@" alias ga="git add" alias gp="git push" +# Simple calculator +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" +}