File tree Expand file tree Collapse file tree 1 file changed +19
-14
lines changed Expand file tree Collapse file tree 1 file changed +19
-14
lines changed Original file line number Diff line number Diff line change 1
1
#! /usr/bin/env bash
2
- # shellcheck disable=SC2155
3
2
4
3
function math::calculate() {
4
+ local expr=" $* "
5
+
5
6
if dependencies::has_bc; then
6
- echo " $* " | bc
7
- elif [[ " $* " == * .* ]] && dependencies::has_awk; then
8
- awk " BEGIN { print ($* ) }"
9
- elif [[ " $* " == * .* ]]; then
10
- # Strip decimal parts and leading zeros
11
- local expression=$( echo " $* " | sed -E ' s/([0-9]+)\.[0-9]+/\1/g' | sed -E ' s/\b0*([1-9][0-9]*)/\1/g' )
12
- local result=$(( expression ))
13
- echo " $result "
14
- else
15
- # Strip leading zeros even for purely integer math
16
- local expression=$( echo " $* " | sed -E ' s/\b0*([1-9][0-9]*)/\1/g' )
17
- local result=$(( expression ))
18
- echo " $result "
7
+ echo " $expr " | bc
8
+ return
9
+ fi
10
+
11
+ if [[ " $expr " == * .* ]]; then
12
+ if dependencies::has_awk; then
13
+ awk " BEGIN { print ($expr ) }"
14
+ return
15
+ fi
16
+ # Downgrade to integer math by stripping decimals
17
+ expr=$( echo " $expr " | sed -E ' s/([0-9]+)\.[0-9]+/\1/g' )
19
18
fi
19
+
20
+ # Remove leading zeros from integers
21
+ expr=$( echo " $expr " | sed -E ' s/\b0*([1-9][0-9]*)/\1/g' )
22
+
23
+ local result=$(( expr ))
24
+ echo " $result "
20
25
}
You can’t perform that action at this time.
0 commit comments