Ir para conteúdo
Fórum Script Brasil
  • 0

Multiplicar Campos


dmetallica

Pergunta

Galera essa duvida tah me matando...

alguém pode fazer um EX bem simples de multiplicar campos(com virgulas!).

eu me ajudar a arruma resse erro:

Aki só funciona com int e numero com virlua ele arredonda. como fazer para q não aredonde.



function valida()
  {
    if (document.form2.peca.value!="" && document.form2.preven.value!="" && document.form2.quantidade.value!="" )
     {
       vl1 = document.form2.peca.value;
       vl2 = document.form2.preven.value;
       vl3 = document.form2.quantidade.value;

       resultado = parseInt(vl1) * parseInt(vl2) * parseInt(vl3);
       document.form2.total.value = resultado;

     }
  }


Se alguém puder ajudar agradeço...

Valleu galera.

Link para o comentário
Compartilhar em outros sites

8 respostass a esta questão

Posts Recomendados

  • 0

Tive que colocar uma função que imita o "Replace" do ASP:

  function multiplica(pNum1, pNum2) {
  	var numero1 = replaceSubstring(pNum1, ",", ".");
  	var numero2 = replaceSubstring(pNum2, ",", ".");
  	var total = parseFloat(numero1) * parseFloat(numero2);
  	alert(total);
  }

	function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

Link para o comentário
Compartilhar em outros sites

  • 0

Valleu fabi. Obrigado smile.gif

Link para o comentário
Compartilhar em outros sites

  • 0

você pode ajudar a adptar para meu problema?

eu não to conseguindo to fazendo assim:


function valida()
  {
    if (document.form2.peca.value!="" && document.form2.preven.value!="" && document.form2.quantidade.value!="" )
     {
        pNum1 = document.form2.peca.value;
        pNum2 = document.form2.preven.value;
        pNum3 = document.form2.quantidade.value;

       var numero1 = replaceSubstring(pNum1, ",", ".");
       var numero2 = replaceSubstring(pNum2, ",", ".");
       var numero3 = replaceSubstring(pNum3, ",", ".");
       var total = parseFloat(numero1) * parseFloat(numero2) * parseFloat(numero3);
       document.form2.total.value = total;
     }
  }

mas tah dando erro!

Link para o comentário
Compartilhar em outros sites

  • 0

diz que um negocio é invalido. ou inexistente.

Link para o comentário
Compartilhar em outros sites

  • 0

alguém pode ajudar???

Link para o comentário
Compartilhar em outros sites

  • 0

Teste este código inteiro:

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<script language="JavaScript">

	function valida() {

  if (document.form2.peca.value!="" && document.form2.preven.value!="" && document.form2.quantidade.value!="" ) {
  	pNum1 = document.form2.peca.value;
  	pNum2 = document.form2.preven.value;
  	pNum3 = document.form2.quantidade.value;

  	var numero1 = replaceSubstring(pNum1, ",", ".");
  	var numero2 = replaceSubstring(pNum2, ",", ".");
  	var numero3 = replaceSubstring(pNum3, ",", ".");
  	var total = parseFloat(numero1) * parseFloat(numero2) * parseFloat(numero3);
  	document.form2.total.value = total;
  }
	}

function replaceSubstring(inputString, fromString, toString) {
  // Goes through the inputString and replaces every occurrence of fromString with toString
  var temp = inputString;
  if (fromString == "") {
     return inputString;
  }
  if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
     while (temp.indexOf(fromString) != -1) {
        var toTheLeft = temp.substring(0, temp.indexOf(fromString));
        var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
        temp = toTheLeft + toString + toTheRight;
     }
  } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
     var midStrings = new Array("~", "`", "_", "^", "#");
     var midStringLen = 1;
     var midString = "";
     // Find a string that doesn't exist in the inputString to be used
     // as an "inbetween" string
     while (midString == "") {
        for (var i=0; i < midStrings.length; i++) {
           var tempMidString = "";
           for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
           if (fromString.indexOf(tempMidString) == -1) {
              midString = tempMidString;
              i = midStrings.length + 1;
           }
        }
     } // Keep on going until we build an "inbetween" string that doesn't exist
     // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
     while (temp.indexOf(fromString) != -1) {
        var toTheLeft = temp.substring(0, temp.indexOf(fromString));
        var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
        temp = toTheLeft + midString + toTheRight;
     }
     // Next, replace the "inbetween" string with the "toString"
     while (temp.indexOf(midString) != -1) {
        var toTheLeft = temp.substring(0, temp.indexOf(midString));
        var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
        temp = toTheLeft + toString + toTheRight;
     }
  } // Ends the check to see if the string being replaced is part of the replacement string or not
  return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

</script>
</head>

<body>
	<form name="form2" id="form2" method="post" action="">
  <input type="text" name="peca" id="peca" />
  <input type="text" name="preven" id="preven" />
  <input type="text" name="quantidade" id="quantidade" />
  <input type="text" name="total" id="total" />

  <input type="button" name="Calcular" value="Calcular" onclick="javascript:valida();" />
	</form>
</body>
</html>

Link para o comentário
Compartilhar em outros sites

  • 0

aham fabi valleu...

era isso mesmo.! smile.gif

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,1k
    • Posts
      651,8k
×
×
  • Criar Novo...