I guess this problem is common for CoffeScript and JavaScript. In my CoffeeScript script I receive a number like 606.0 over a websocket. Thus it comes as a string, being extracted from the JSON that arrived from the host. Now I want to work with this number, say, add something to it like:
# @x is the number presented as a string
@xx = @x + 100
console.log("res=" + @xx)
and what I get is:
res=606.0100
So it gets added as a string! And if I change the code a little, “saying” that 100 is float:
# @x is the number presented as a string
@xx = @x + 100.0
console.log("res=" + @xx)
The result is still the same.
My question is – how to explain to CoffeScript/JavaScript that this is a number, and not a string?
To cast number from string just prepend a
+:http://jsfiddle.net/elclanrs/d77uq/