Improve is_float to accept standard form notation

This commit is contained in:
Gregory Chamberlain 2020-09-23 23:58:42 +01:00
parent 2d2d96f63b
commit 83a8538479

View file

@ -894,10 +894,11 @@ var=$((var2 > var ? var2 : var))
is_float() { is_float() {
# Usage: is_float "number" # Usage: is_float "number"
# The test checks to see that the input contains # Printing as '%d' must fail in order to filter out whole numbers;
# a '.'. This filters out whole numbers. # then printing as '%f' will succeed if and only if '$1' is a float.
[ -z "${1##*.*}" ] && # This works for both decimal point form (i.g. 3.14) and standard form
printf %f "$1" >/dev/null 2>&1 # (e.g. 314e-2).
{ ! printf %d "$1" && printf %f "$1" ; } >/dev/null 2>&1
} }
``` ```