diff options
author | alrra <alrraa@gmail.com> | 2013-01-06 00:54:10 +0200 |
---|---|---|
committer | Mathias Bynens <mathias@qiwi.be> | 2013-01-06 09:47:28 +0100 |
commit | 3ea3614be94960fa04410c8cafc9ba2e982afb50 (patch) | |
tree | 5391bc457f78e28958ca595a46e1cd5c1dc8f9c9 | |
parent | b7389dc0babd93d599b2b55bb65e3a5ec7ddaa59 (diff) | |
download | dotfiles-3ea3614be94960fa04410c8cafc9ba2e982afb50.tar.gz dotfiles-3ea3614be94960fa04410c8cafc9ba2e982afb50.tar.bz2 dotfiles-3ea3614be94960fa04410c8cafc9ba2e982afb50.zip |
.functions: Add `calc`
-rw-r--r-- | .functions | 18 |
1 files changed, 18 insertions, 0 deletions
@@ -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 "$@" |