File tree Expand file tree Collapse file tree 1 file changed +9
-3
lines changed Expand file tree Collapse file tree 1 file changed +9
-3
lines changed Original file line number Diff line number Diff line change 1
1
#! /usr/bin/env bash
2
+ # shellcheck disable=SC2155
2
3
3
4
function math::calculate() {
4
5
if dependencies::has_bc; then
5
6
echo " $* " | bc
6
7
elif [[ " $* " == * .* ]] && dependencies::has_awk; then
7
- # Use awk for floating point calculations when bc is unavailable
8
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 "
9
14
else
10
- # Fallback to shell arithmetic which has good integer precision
11
- local result=$(( $* ))
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 ))
12
18
echo " $result "
13
19
fi
14
20
}
You can’t perform that action at this time.
0 commit comments