cast.ToFloat throws an error

I have the following data:

{{ printf "%T" $resultprice }}
{{ printf $resultprice }}

The output is:

string 
402.30 

I feed this data into the expression:

{{ $resultpriceFloat := cast.ToFloat $resultprice }}

It works for 1-2 minutes and shows the output:

float64 
402.30

But then it crashes and throws an error:

execute of template failed at <float $resultprice>: error calling float: unable to cast "%!!(MISSING)f(int=00)" of type string to float64 

What am I doing wrong?
Thanks!

In the above, I suspect you have set

$resultPrice

to

printf "%s" nil

Because this produces the same result:

{{ cast.ToFloat (printf "%s") }}

You have a nil value somewhere.

Thank you Joe! I didn’t have the nil value, but my preceding conversions were a bit too complicated. I streamlined them and it started working.

1 Like