mirror of
https://github.com/dylanaraps/pure-sh-bible
synced 2024-06-08 16:52:31 +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() {
|
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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue