diff options
author | Ben Sima <ben@bsima.me> | 2022-09-02 09:59:16 -0400 |
---|---|---|
committer | Ben Sima <ben@bsima.me> | 2022-09-02 09:59:16 -0400 |
commit | e93f27b4b532421e2f95d9bcae4cd2031a4d8d20 (patch) | |
tree | 3a4dfe9cf16bc585418efebcab1ef147d86c101e | |
parent | 35416ace2f49f05f44b21e163ed0bb544f0ca971 (diff) |
add calc script
-rwxr-xr-x | calc | 16 |
1 files changed, 16 insertions, 0 deletions
@@ -0,0 +1,16 @@ +#!/usr/bin/env bash +# Simple calculator +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" |