aboutsummaryrefslogtreecommitdiffstats
path: root/.functions
diff options
context:
space:
mode:
authoralrra <alrraa@gmail.com>2013-01-06 00:54:10 +0200
committerMathias Bynens <mathias@qiwi.be>2013-01-06 09:47:28 +0100
commit3ea3614be94960fa04410c8cafc9ba2e982afb50 (patch)
tree5391bc457f78e28958ca595a46e1cd5c1dc8f9c9 /.functions
parentb7389dc0babd93d599b2b55bb65e3a5ec7ddaa59 (diff)
downloaddotfiles-3ea3614be94960fa04410c8cafc9ba2e982afb50.tar.gz
dotfiles-3ea3614be94960fa04410c8cafc9ba2e982afb50.tar.bz2
dotfiles-3ea3614be94960fa04410c8cafc9ba2e982afb50.zip
.functions: Add `calc`
Diffstat (limited to '.functions')
-rw-r--r--.functions18
1 files changed, 18 insertions, 0 deletions
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 "$@"