mirror of
https://github.com/dylanaraps/pure-sh-bible
synced 2024-06-09 01:02:30 +02:00
Merge 960c2a84d4
into 26bc7e8d6c
This commit is contained in:
commit
43a1a6e1e8
22
README.md
22
README.md
|
@ -894,21 +894,24 @@ 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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**Example Usage:**
|
**Example Usage:**
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ is_float 1 && echo true
|
$ is_float 3 && echo true
|
||||||
$
|
|
||||||
|
|
||||||
$ is_float 1.1 && echo true
|
$ is_float 3.14 && echo true
|
||||||
$ true
|
true
|
||||||
|
|
||||||
|
$ is_float 314e-2 && echo true
|
||||||
|
true
|
||||||
```
|
```
|
||||||
|
|
||||||
## Check if a number is an integer
|
## Check if a number is an integer
|
||||||
|
@ -926,10 +929,9 @@ is_int() {
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ is_int 1 && echo true
|
$ is_int 1 && echo true
|
||||||
$ true
|
true
|
||||||
|
|
||||||
$ is_int 1.1 && echo true
|
$ is_int 1.1 && echo true
|
||||||
$
|
|
||||||
```
|
```
|
||||||
|
|
||||||
# TRAPS
|
# TRAPS
|
||||||
|
|
Loading…
Reference in a new issue