| Viewed 28 times

how to convert variable to float in js


how to convert variable to float in js

1 Answer
Mohammad Qandeel
Answered
0

in javascript, we use parseFloat() function to convert any variable to float 

look to this example 

<script>	
	console.log(parseFloat(5));
	console.log(parseFloat(5.5));
	console.log(parseFloat("5"));
	console.log(parseFloat("5.5"));
	console.log(parseFloat("5.5  "));
	console.log(parseFloat("  5.5  "));
	console.log(parseFloat("  5.5 ok "));
	console.log(parseFloat(" ok 5.5  "));
	console.log(parseFloat(" ok 5.5  ok"));
</script>	

the output will be

5
5.5
5
5.5
5.5
5.5
5.5
NaN
NaN