Why does setting a variable to one, return 1.0?

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

Why does setting a variable to one, return 1.0?

Postby joe26 » Fri Aug 31, 2018 1:13 am

Setting variable to an integer 1 returns 1.0, but setting the same variable to 2 returns 2.

num = 1
num = 2
This can be run from the console.

application.output(num) results in a real number;
num entered in the console shows real if it equals 1 and integer if greater than one.
application.output(num) always shows real
application.output('num '+num) always show integer


What's up with this? Nature of javascript always handling numbers as real?

thanks,
--Joe.
joe26
 
Posts: 172
Joined: Wed Jun 19, 2013 10:30 pm

Re: Why does setting a variable to one, return 1.0?

Postby omar » Fri Aug 31, 2018 1:58 pm

Hi Joe,

JavaScript Numbers are Always 64-bit Floating Point
Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc.
JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard. (source w3schools.com)

For more info see: https://www.w3schools.com/js/js_numbers.asp

In your application output string concatenation is used which causes implicit conversion of the number to a string. This is also explained in the page linked above.
Intrasoft, Founder
Omar van Galen
omar@intrasoft.nl
+31-(0)6-21234586
Servoy Developer
omar
 
Posts: 377
Joined: Sat Feb 12, 2011 4:51 pm
Location: Intrasoft, The Netherlands

Re: Why does setting a variable to one, return 1.0?

Postby joe26 » Fri Aug 31, 2018 2:52 pm

Hi Omar,

Thank You.

Helps a lot.
joe26
 
Posts: 172
Joined: Wed Jun 19, 2013 10:30 pm


Return to Programming with Servoy

Who is online

Users browsing this forum: alasdairs and 16 guests