From 83a853847981ede3b463a042a9e888cc2f3cd064 Mon Sep 17 00:00:00 2001 From: Gregory Chamberlain Date: Wed, 23 Sep 2020 23:58:42 +0100 Subject: [PATCH 1/2] Improve is_float to accept standard form notation --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1fd0155..de37eec 100644 --- a/README.md +++ b/README.md @@ -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 } ``` From 960c2a84d46bfd873f819e5aefe3b4c9f7323bc9 Mon Sep 17 00:00:00 2001 From: Gregory Chamberlain Date: Thu, 24 Sep 2020 00:04:37 +0100 Subject: [PATCH 2/2] Adjust usage examples for is_float and is_int --- README.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index de37eec..51d7cdb 100644 --- a/README.md +++ b/README.md @@ -905,11 +905,13 @@ is_float() { **Example Usage:** ```shell -$ is_float 1 && echo true -$ +$ is_float 3 && echo true -$ is_float 1.1 && echo true -$ true +$ is_float 3.14 && echo true +true + +$ is_float 314e-2 && echo true +true ``` ## Check if a number is an integer @@ -927,10 +929,9 @@ is_int() { ```shell $ is_int 1 && echo true -$ true +true $ is_int 1.1 && echo true -$ ``` # TRAPS