In my code, the value of a particular var can originate from any one of a number of different json sources. For some of those sources, the json element concerned will be a string (e.g. "temp": "10.2"
), while for other sources the json element will already be a float (e.g. "temp": 10.2
).
Does it do any harm (is anything likely to break) if I just pass the json element (from whatever source) through a parseFloat()
, even if it's already a float? It seems to work; I'm just thinking about good/bad practice and possible breakage in future or on a different platform.
Thanks.
In my code, the value of a particular var can originate from any one of a number of different json sources. For some of those sources, the json element concerned will be a string (e.g. "temp": "10.2"
), while for other sources the json element will already be a float (e.g. "temp": 10.2
).
Does it do any harm (is anything likely to break) if I just pass the json element (from whatever source) through a parseFloat()
, even if it's already a float? It seems to work; I'm just thinking about good/bad practice and possible breakage in future or on a different platform.
Thanks.
Share Improve this question edited Jul 23, 2015 at 7:39 Salman Arshad 273k84 gold badges444 silver badges534 bronze badges asked Jul 22, 2015 at 10:31 drmrbrewerdrmrbrewer 13.2k24 gold badges98 silver badges212 bronze badges 06 Answers
Reset to default 1You should be able to call parseFloat() on a float or a string without any problems. If it is a float already, it's converted to a string first, and then to a float again, so it's a little less efficient, but it shouldn't matter too much.
You should still check the result for NaN, in case there's something unexpected in the data.
The most appropriate method to convert any datatype to a number is to use the Number
function:
In a non-constructor context (i.e., without the
new
operator),Number
can be used to perform a type conversion.
Number("1234") // 1234
Number(1234) // 1234
This method differs from parseFloat
in these ways at least:
- Number function does not perform "double-conversion" if the input is already a number (ref)
- Parse float converts the input to a string then extracts the number (ref)
- Number function returns mon sense values for most datatypes e.g.
Number(true)
yields 1- Parse float uses the string value of input so
parseFloat(true)
tries to parse number from"true"
and yields NaN
- Parse float uses the string value of input so
- Number function fails when input string is an invalid number e.g.
Number("123abc")
yields NaN- Parse float tries to parse as much of a number as possible e.g.
parseFloat("123abc")
yields 123
- Parse float tries to parse as much of a number as possible e.g.
If you are sure the value is always a valid number, you should use Number(stringOrNumber)
.
If you need some additional safety using parseFloat()
you could also write your own function which is also performance optimized:
function toFloat(value) {
return typeof value === 'number' ? value : parseFloat(value);
}
I also created a jsPerf test case that shows the performance is >30% better than the plain parseFloat()
for a 1:1 ratio between strings and numbers as input values.
Nope there is no problem with passing a number to it
MDN says as long as it can be converted to a number, nothing breaking should happen.
If the first character cannot be converted to a number, parseFloat returns NaN.
As an alternative, you could use the unary operator +
which does basically the same thing as parseFloat
and also returns NaN
if it didn't work.
For instance:
var myFloat = +('10.5');
var myOtherFloat = parseFloat('10.5', 10);
var alreadyAFloat = parseFloat(10.5, 10);
console.log(myFloat === myOtherFloat && myOtherFloat === alreadyAFloat); // true
Wether it's a float
or a String
using parseFloat()
is much safer to avoid all kind of errors.
As you said it will always work, but if you enforce it to be a float you will avoid getting any Exception.
For Example:
Both
parseFloat('10.2', 10)
andparseFloat(10.2, 10)
will work perfectly and will give you the same result which is10.2
.
Personally I can't see this being a problem what so ever, to be honest I would always use the parsefloat() for one reason, and that is safety. You can never be to sure what may happen, so always predict the worse :D