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 11#! /usr/bin/env bash
2+ # shellcheck disable=SC2155
23
34function math::calculate() {
45 if dependencies::has_bc; then
56 echo " $* " | bc
67 elif [[ " $* " == * .* ]] && dependencies::has_awk; then
7- # Use awk for floating point calculations when bc is unavailable
88 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 "
914 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 ))
1218 echo " $result "
1319 fi
1420}
You can’t perform that action at this time.
0 commit comments