# Simply add 2 float (with 2 decimal) and sometimes get a 13rd decimal digit

Hello,

Strange behaviour when I add 2 float (being with 2 decimal or converted from integer).
It works fine. Values are correct (ex 136.22) , but sometimes I get a 13rd digit (ex : 2796.2200000000003 instead of 2796.22)
This happens on some cases, and there is no pattern at all because source numbers are random ones.

``````{{- \$valOrigineCol := float (index (\$.Scratch.Get "listeTotaux1Categ_Temp") \$j) -}}
{{- \$val2add := float (lang.NumFmt 2 \$val_US_str "- .") -}}
``````

Problem is with \$valResult (when this happens \$valOrigineCol & \$val2add are OK)

• 153.68 + 268 = 421.68
• 1136.22 + 1660 = 2796.2200000000003

I’m puzzled as I didn’t found any similar case searching the web/Hugo forum.
Mac OS + Hugo 0.37.1

That is normal, as floating point numbers are only very close to the precise value you’re looking for, which is really cool for making huge calculations faster. But in your case, you know how many digits you want behind the dot (2), so you may use integers to do your calculations, and then do `mod` by 100 (modulo) to get the integer part of your number.

Like so:

``````{{ \$a := 1422 }}
{{ \$b := 231 }}
{{ \$total := add \$a \$b }}
{{ \$int := mod \$total 100 }}
{{ \$rest := sub \$total \$int }}
That cost {{ \$int }}.{{ \$rest }}\$.
``````

Hope that helps 2 Likes

Yes thanks. I was thinking about that type of solution as a hack But I’ll do it as it will be consistent. Thanks for the clear explanation.