In Javascript (trying in Firefox 4)
"" + 8.1
gives "8.1" (a string). But
"" + 8.0
gives "8" (also a string), but is there a way to make it give "8.0" instead like it did for 8.1?
In Javascript (trying in Firefox 4)
"" + 8.1
gives "8.1" (a string). But
"" + 8.0
gives "8" (also a string), but is there a way to make it give "8.0" instead like it did for 8.1?
You need a proper printf style extension for Javascript if you want to show numbers with a particular precision - you can't do it with plain type coercion.
Search for "javascript printf" here and on Google - there's plenty of suitable references.
Javascript has internal type convertion (called loose typing) so that it converts 8.0 to 8 by default. Javascript has no separate datatypes for float numbers and integers like other languages. It has just the type Number. You can print numbers using printf (e.g. from a jquery plugin or you can convert the Number to a string with fixed width: (8).toFixed(1) which creates a string, so you don't have to use "" + ...