From c1c45eeb967088b17ec7b95f4485ad79fa923503 Mon Sep 17 00:00:00 2001 From: Tobias Manske Date: Thu, 23 Jun 2022 12:13:24 +0200 Subject: [PATCH] ZSH: calc function --- .zshrc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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" +}