Problem adding together two variables

Hello,

I’m struggling with what I thought was a simple task, adding together two numbers found in two different variables. Each of these variables are stored as as numbers (not text).

One variable is found in Data and has the value of 9.5

.Site.Data.pricing.spoon

The other variable is found in Front Matter and has the value of 1.5

.Params.base_plus

My goal is to add these two numbers together. So, I used the following code:

{{ $price := float .Site.Data.pricing.spoon | lang.FormatNumber 2 }}
{{ $base_plus := float .Params.base_plus | lang.FormatNumber 2 }}

And when I add them together with add, I would hope to get 11 (9.5 + 1.5)

{{ add $price $base_plus }}

But what I get is a concatenation of the two variables: 9.501.50

I feel like I’m missing something simple here. How do I get those two numbers to sum, not “concatenate”.

lang.FormatNumber produces a string, so you are adding two strings together (concatenation). Do this instead:

{{ $x := 9.5 }}
{{ $y := 1.5 }}
{{ add $x $y | lang.FormatNumber 2 }}

Thank you! That was the insight I was missing, that the output of FormatNumber was a string.

FYI, I didn’t see mention of FormatNumber producing a string. Would it make sense to do a pull request and put that in the docs?

If it wasn’t clear to you by reading the docs, then do a pull request and make it clear. This is the way.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.