Skip to content

Commit

Permalink
#99 float better tested and fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Oct 5, 2023
1 parent 1c73c32 commit 2de0561
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion help/float.sh
Expand Up @@ -24,4 +24,10 @@ set -e
set -o pipefail

num=$(cat)
printf '%.8f' "${num}" | sed -e 's/0\+$//' | sed -e 's/\.$//'

if [ "${num}" == 'NaN' ]; then
printf '%s' "${num}"
exit
fi

(printf '%.8f' "${num}" 2>/dev/null || echo 0) | sed -e 's/0\+$//' | sed -e 's/\.$//'
15 changes: 15 additions & 0 deletions tests/help/test-float.sh
Expand Up @@ -32,6 +32,21 @@ echo "👍🏻 Corrected longer floating point number"
test "$(echo '256' | "${LOCAL}/help/float.sh")" = '256'
echo "👍🏻 Corrected integer number"

test "$(echo '09' | "${LOCAL}/help/float.sh")" = '9'
echo "👍🏻 Corrected integer number with leading zero"

test "$(echo '' | "${LOCAL}/help/float.sh")" = '0'
echo "👍🏻 Corrected integer number with empty text"

test "$(echo ' ' | "${LOCAL}/help/float.sh")" = '0'
echo "👍🏻 Corrected integer number with spaces"

test "$(echo 'Blank' | "${LOCAL}/help/float.sh")" = '0'
echo "👍🏻 Corrected integer number with text input"

test "$(echo 'NaN' | "${LOCAL}/help/float.sh")" = 'NaN'
echo "👍🏻 Corrected integer number with NaN"

test "$(echo '.000000099' | "${LOCAL}/help/float.sh")" = '0.0000001'
echo "👍🏻 Corrected small precision number"

0 comments on commit 2de0561

Please sign in to comment.