Passing a number in parseFloat () instead of a string

In my code, the value of a specific var can come from any of several different json sources. For some of these sources, the corresponding json element will be a string (for example, "temp": "10.2" ), while for other sources the json element will already be a float (for example, "temp": 10.2 ).

Does it do any harm (something might break) if I just pass the json element (from any source) through parseFloat() , even if it is already a float? It seems to work; I just think about good / bad practice and a possible breakdown in the future or on another platform.

Thanks.

+7
json javascript numbers parsefloat
source share
6 answers

You can be able to call parseFloat () on a float or string without any problems. If it is already a float, it is first converted to a string and then to a float, so it is a little less efficient, but that should not matter much.

You should check the result for NaN if there is something unexpected in the data.

+1
source share

The most suitable way to convert any data type to number is to use the Number function:

In the context of a non-constructor (that is, without the new operator), Number can be used to convert types.

 Number("1234") // 1234 Number(1234) // 1234 

This method differs from parseFloat following ways:

  • The 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 )
  • The number function returns common sense values ​​for most data types, for example. Number(true) gives 1
    • Parse float uses the string value of the input, so parseFloat(true) tries to parseFloat(true) number from "true" and gives NaN
  • The number function does not work when the input line is an invalid number, for example. Number("123abc") gives NaN
    • Parse float tries to parse as many numbers as possible, for example. parseFloat("123abc") gives 123
+1
source share

If you are sure that the value is always a valid number, you should use Number(stringOrNumber) .

If you need extra security with parseFloat() , you can also write your own function, which is also optimized for performance:

 function toFloat(value) { return typeof value === 'number' ? value : parseFloat(value); } 

I also created a jsPerf test case that shows that performance is 30% better than regular parseFloat() for a 1: 1 ratio between strings and numbers as input values.

+1
source share

Personally, I do not see that this is a problem, which is always the case, to be honest, I will always use parsefloat () for one reason, and this is security. You can never be sure what will happen, so always predict the worst: D

0
source share

No problem handing him a number

MDN says that while it can be converted to a number, nothing should happen.

If the first character cannot be converted to a number, parseFloat returns NaN.

Alternatively, you can use the unary + operator, which does basically the same thing as parseFloat , and also returns NaN if it doesn't work.

For example:

 var myFloat = +('10.5'); var myOtherFloat = parseFloat('10.5', 10); var alreadyAFloat = parseFloat(10.5, 10); console.log(myFloat === myOtherFloat && myOtherFloat === alreadyAFloat); // true 
0
source share

When using float or String using parseFloat() much safer to avoid all errors.

As you said, it will always work, but if you use it as a float, you will not get any exception.

Example:

Both parseFloat('10.2', 10) and parseFloat(10.2, 10) will work fine and will give you the same result as 10.2 .

0
source share

All Articles