Convert string into float in javascript & ReactJs – Code Example

Use parseFloat() function to covert string into float in javascript & ReactJs. The digit before decimal must start with a digit otherwise it will return NaN. Check the code example –

function circumference(r) {
  return parseFloat(r) * 2.0 * Math.PI;
}

console.log(circumference(4.567));
// expected output: 28.695307297889173

console.log(circumference('4.567abcdefgh'));
// expected output: 28.695307297889173

console.log(circumference('4.ere567abcdefgh'));
// expected output: 25.132741228718345

console.log(circumference('4sdf.ere567abcdefgh'));
// expected output: 25.132741228718345

console.log(circumference('ert4.ere567abcdefgh'));
// expected output: NaN

console.log(circumference('abcdefgh'));
// expected output: NaN

Source: MSDN