mirror of
https://github.com/dylanaraps/pure-sh-bible
synced 2024-05-18 05:16:40 +02:00
Improve is_float to accept standard form notation
This commit is contained in:
parent
2d2d96f63b
commit
83a8538479
|
@ -894,10 +894,11 @@ var=$((var2 > var ? var2 : var))
|
|||
is_float() {
|
||||
# Usage: is_float "number"
|
||||
|
||||
# The test checks to see that the input contains
|
||||
# a '.'. This filters out whole numbers.
|
||||
[ -z "${1##*.*}" ] &&
|
||||
printf %f "$1" >/dev/null 2>&1
|
||||
# Printing as '%d' must fail in order to filter out whole numbers;
|
||||
# then printing as '%f' will succeed if and only if '$1' is a float.
|
||||
# This works for both decimal point form (i.g. 3.14) and standard form
|
||||
# (e.g. 314e-2).
|
||||
{ ! printf %d "$1" && printf %f "$1" ; } >/dev/null 2>&1
|
||||
}
|
||||
```
|
||||
|
||||
|
|
Loading…
Reference in a new issue