From 3ea3614be94960fa04410c8cafc9ba2e982afb50 Mon Sep 17 00:00:00 2001 From: alrra Date: Sun, 6 Jan 2013 00:54:10 +0200 Subject: .functions: Add `calc` --- .functions | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to '.functions') diff --git a/.functions b/.functions index c2c1556..db887fa 100644 --- a/.functions +++ b/.functions @@ -1,3 +1,21 @@ +# 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" +} + # Create a new directory and enter it function mkd() { mkdir -p "$@" && cd "$@" -- cgit v1.2.3