Avoiding unexpected long float results when adding two small floats

I was adding two currency amounts to display a total amount in a web flow. This is the expression I was using:

"Total: ${{
  payload.amount + order_info.shipping.amount / 100
}}"

But it would sometimes return long floats instead of the expected results, as in this screen shot:

It turns out that because Math and floats, this is a valid output. Smarter people that me, please feel free to chime in on the reasons here…

All that said, the solve was to use the FOMAT_NUMBER function with the second parameter of "$.2f" to save the day. The $ represents to append a $ to it, the .2 stops it at two spaces, and the f is for ‘fill’, which completes the number to 2 decimal places instead of leaving it at a single decimal place or no decimals for whole numbers.

"Subtotal: {{
  FORMAT_NUMBER(
    payload.amount + order_info.shipping.amount / 100, "$.2f"
  )
}}"
1 Like