I am very new to Velo and, having followed a number of tutorials, have just ventured into writing my very first piece of my own and am getting unexpected results.
My intention was to write a function which increments the opacity of a box each time it is clicked (simple I know!). The problem is not with the function itself, but with simple addition. I set a variable “opac” to an initial value of 0.1, then each time the box is clicked, the code should add 0.1 to “opac”. As a check, I am using console log to display the value of “opac” and these are the results:

Hello. I would try toFixed(1) and see if the results are accurate enough for your needs. You can also google something like “adding decimals in Javascript” and you should see many results on how to approach this. Math is fun

Thank you both for your valuable advice. Both suggestions work great but, as a newbie, I’m amazed at how such a simple calculation can result in an inaccurate result. Every day is a school day
Thanks guys, I really appreciate you taking the time to help out a newbie.

This is actually an issue with Binary and not necessarily JavaScript. In binary decimal numbers are stored in what is known as BCD or Binary Coded Decimal values. This allows each digit to be assigned a fixed bit value. You can read more about that here: Binary-coded decimal - Wikipedia

0.1 can not accurately be converted into binary. Doing so gives us an infinite repeating number. You can see that represented below:

that line of numbers will repeat forever if allowed. (I.E. 0.000110001100011…)
This leaves us with a problem when converting back to decimal as we will never get exactly the same number that we are expecting.