ZSH: calc function

This commit is contained in:
Tobias Manske 2022-06-23 12:13:24 +02:00
parent 68fca270ca
commit c1c45eeb96
Signed by: tobias
GPG Key ID: E83C743C1FC2F79A

17
.zshrc
View File

@ -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"
}