Convert String to Float

Asked

Viewed 270 times

1

I’m trying to convert string to float to calculate the advantage between gasoline and ethanol, but is not returning result in Screen.

Below the code HTML and JS:

var etanol, gasolina;

function Vantagem(){
	etanol = parseFloat(txtEtanol.value.replace(",","."));
	gasolina = parseFloat(txtGasolina.value.replace(",","."));

	if (etanol < 0.7 * gasolina) {
		document.getElementById('status').src="img/medidorEtanol.jpg";

	} else{
		document.getElementById('status').src="img/medidorGasolina.jpg";
	}
}

function Limpar(){
	document.getElementById('status').src="img/medidorNeutro.jpg";
}
<!DOCTYPE html>

<html>

	<head>
		<meta charset = 'UTF-8'>

		<title> Calculadora de Combustível </title>


	</head>

	<body>

		<header>

			<h1> Calculadora de Combustível </h1>
	

		</header>

		<img src='img/medidorNeutro.jpg' id="status">


		<form name="frmFlex">

			Etanol: <input type="text" name="txtEtanol" placeholder="Etanol"> <br/>
			Gasolina: <input type="text" name="txtGasolina" placeholder="Gasolina"> <br/>

			<button onclick="Vantagem()"> Vantagem </button> <br/>
			<button onclick="Limpar()"> Limpar </button> <br/>

		</form>
    
    
			<script type="text/javascript" src="script.js"></script>

	</body>

</html>

  • Use the method etanol = +txtEtanol.value.replace(",",".");&#xA; gasolina = +txtGasolina.value.replace(",",".");&#xA;&#xA; instead of parseFloat

1 answer

3


Your code has some problems:

  • on the line etanol = parseFloat(txtEtanol.value.replace(",","."));, where the variable was defined txtEtanol?

  • in its input, it would not be better to define "txtEtanol" as id in place of name?

  • why declare the global variables var etanol, gasolina; if you’re only using in Function?

  • to compare and ensure operation with gasoline, add a parenthesis here: if (etanol < 0.7 * gasolina)

To simplify a little bit the code, I created this example:

function Vantagem() {
    var etanol = parseFloat(document.getElementById('txtEtanol').value.replace(",", "."));
    var gasolina = parseFloat(document.getElementById('txtGasolina').value.replace(",", "."));

    if (etanol < (0.7 * gasolina)) {
        document.getElementById('status').src = "img/medidorEtanol.jpg";
        alert('medidorEtanol'); // apenas para testar

    } else {
        document.getElementById('status').src = "img/medidorGasolina.jpg";
        alert('medidorGasolina'); // apenas para testar
    }
}

function Limpar() {
    document.getElementById('status').src = "img/medidorNeutro.jpg";
}
<header>
   <h1> Calculadora de Combustível </h1>
</header>
<img src='img/medidorNeutro.jpg' id="status">

   Etanol: <input type="text" id="txtEtanol" placeholder="Etanol"> <br/>
   Gasolina: <input type="text" id="txtGasolina" placeholder="Gasolina"> <br/>
   <button onclick="Vantagem()"> Vantagem </button> <br/>
   <button onclick="Limpar()"> Limpar </button> <br/>

  • If he changed Parsefloat to + would be the same thing?

  • as what put in the comment? etanol = +txtEtanol.value.replace(",",".")? I don’t know this syntax so I don’t know if it works, but it doesn’t seem clear to me, but anyway will give error by txtEtanol is not defined

  • Work. This syntax turns into number.

  • @Maurydeveloper With the + would be the same because + followed by a string the Javascript tries to convert to number as if it had done + Number(texto) which will either work whether the text has an integer number or a float

  • Yes that’s right. I wanted help,.

  • 1

    interesting, I’ve never done it that way, but somehow I find the parseFloat clearer, as well as Number :)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.