ZSH: calc function
This commit is contained in:
parent
68fca270ca
commit
c1c45eeb96
17
.zshrc
17
.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"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user