Search code examples
javascriptnumerical

Javascript to convert string to number?


var str = '0.25';

How to convert the above to 0.25?


Solution

  • There are several ways to achieve it:

    Using the unary plus operator:

    var n = +str;
    

    The Number constructor:

    var n = Number(str);
    

    The parseFloat function:

    var n = parseFloat(str);