aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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 "$@"