Browse Source

Add support for float currency values

Alois Mahdal 3 years ago
parent
commit
1cc61972fd
1 changed files with 8 additions and 11 deletions
  1. 8
    11
      bin/czkrates

+ 8
- 11
bin/czkrates View File

@@ -33,15 +33,12 @@ usage() {
33 33
 
34 34
 type_of() {
35 35
     #
36
-    # Print 'int' if $1 is an integer, 'nil' if empty; 'str' otherwise
36
+    # Print 'num' if $1 is a number (float or integer), 'nil' if empty; 'str' otherwise
37 37
     #
38
-    local es_p es_n
39 38
     test -z "$1" && echo nil && return
40
-    test "$1" -ge 0 2>/dev/null; es_p=$?    # assuming positive
41
-    test "$1" -lt 0 2>/dev/null; es_n=$?    # assuming negative
42
-    test $es_p -eq 2 && echo str && return
43
-    test $es_n -eq 2 && echo str && return
44
-    echo int
39
+    grep -qxEe '-?[0-9]*[.][0-9]+' <<<"$1" && echo num && return
40
+    grep -qxEe '-?[0-9]+' <<<"$1"          && echo num && return
41
+    echo str
45 42
 }
46 43
 
47 44
 parse_rate1() {
@@ -114,11 +111,11 @@ main() {
114 111
     esac done
115 112
     Uri+="?date=$(date -d "$Date" +%d.%m.%Y)"
116 113
     case "$(type_of "$1"):$(type_of "$2")" in
117
-        int:int|str:str)    usage ;;
118
-        int:nil|nil:int)    Mult="$1$2"          ;;
114
+        num:num|str:str)    usage ;;
115
+        num:nil|nil:num)    Mult="$1$2"          ;;
119 116
         str:nil|nil:str)    Curr="$1$2"          ;;
120
-        str:int)            Curr="$1"; Mult="$2" ;;
121
-        int:str)            Curr="$2"; Mult="$1" ;;
117
+        str:num)            Curr="$1"; Mult="$2" ;;
118
+        num:str)            Curr="$2"; Mult="$1" ;;
122 119
     esac
123 120
     Curr="${Curr:-$CZKRATES_DEFAULT_CURRENCY}"
124 121
     Curr="${Curr^^}"