# \[Solved\] Returned Float64 is received as union in calling function?

**URL:** <https://forum.crystal-lang.org/t/solved-returned-float64-is-received-as-union-in-calling-function/1772>\
**Category:** Help & Support\
**Created:** [February 29, 2020, 2:15pm UTC](https://forum.crystal-lang.org/t/solved-returned-float64-is-received-as-union-in-calling-function/1772 "2020-02-29T14:15:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kees](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kees/32/914_2.png) [@kees](https://forum.crystal-lang.org/u/kees)\
**Post date:** [February 29, 2020, 2:15pm UTC](https://forum.crystal-lang.org/t/solved-returned-float64-is-received-as-union-in-calling-function/1772/1 "2020-02-29T14:15:13Z")

</div>

Hi,

I have the following situation, the function `find_rate` gets a value of JSON::Any and converts it in a Float64 (a typeof of this variable confirms this), being returned, it becomes a union of type (Bool | Float64) and can not be used for calculation.  
When tryring to make it a Float64 with appending `.to_f64`, I get the following error:  
`Error: undefined method 'to_f64' for Bool (compile-time type is (Bool | Float64))`  
Is this normal procedure?  
How to solve this?

Here is the stripped down code:

```crystal
require "http/client"
require "json"

# check for valid currency and return rate
def find_rate(items, cur)
   rate : Float64 = 0.0
   if cur == "EUR"
      return 1.0_f64
   end
   if items["rates"][cur]?
      puts "Currency #{cur}: #{items["rates"][cur]}"
   else
      puts "Currency #{cur} not found"
      return false
   end
   rate1 = items["rates"][cur]
   puts "typeof(rate1): #{typeof(rate1)} in function"
   rate = rate1.to_s.to_f64
   puts "typeof(rate): #{typeof(rate)} in function"
   return rate
end

response = HTTP::Client.get "https://api.exchangeratesapi.io/latest"
items = JSON.parse(response.body)

cur_in = "EUR"
cur_out = "USD"
amount_in = 10.0_f64
#rate_out = find_rate(items, cur_out).to_f64
rate_out = find_rate(items, cur_out)
#rate_out = 1.0977_f64
puts "rate: #{rate_out} #{typeof(rate_out)} after function call"
#amount_out = amount_in * rate_out
#print "%.2f #{cur_in} = %.2f #{cur_out}\n" % [amount_in, amount_out]

```

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [February 29, 2020, 2:30pm UTC](https://forum.crystal-lang.org/t/solved-returned-float64-is-received-as-union-in-calling-function/1772/2 "2020-02-29T14:30:49Z")

</div>

The reasoning is your method can either returns `false` or `rate`, which would make the return type be a union of `Bool | Float64`.

One solution would be to raise an exception instead of false. Another would be add some conditional logic to make sure the response is the correct type before doing additional logic on it.

something like

```auto
if rate_out.is_a? Float64
  # Do your logic there
end

```

---

<div class="post-metadata">

**Author:** ![kees](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kees/32/914_2.png) [@kees](https://forum.crystal-lang.org/u/kees)\
**Post date:** [February 29, 2020, 2:39pm UTC](https://forum.crystal-lang.org/t/solved-returned-float64-is-received-as-union-in-calling-function/1772/3 "2020-02-29T14:39:49Z")

</div>

@Blacksmoke16 thank you, that makes sense, I hadn’t looked at it that way.
