segunda-feira, 8 de setembro de 2014

Java Script - Formatação Casas Decimais

O exemplo a seguir formata o resultado do calculo com as casas decimais:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Java Scrit - Calcular</title>
<script type="text/javascript">
function maskIt(w,e,m,r,a){
// Cancela se o evento for Backspace
if (!e) var e = window.event
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
// Variáveis da função
var txt  = (!r) ? w.value.replace(/[^\d]+/gi,'') : w.value.replace(/[^\d]+/gi,'').reverse();
var mask = (!r) ? m : m.reverse();
var pre  = (a ) ? a.pre : "";
var pos  = (a ) ? a.pos : "";
var ret  = "";
if(code == 9 || code == 8 || txt.length == mask.replace(/[^#]+/g,'').length) return false;
// Loop na máscara para aplicar os caracteres
for(var x=0,y=0, z=mask.length;x<z && y<txt.length;){
if(mask.charAt(x)!='#'){
ret += mask.charAt(x); x++; } 
else {
ret += txt.charAt(y); y++; x++; } }
// Retorno da função
ret = (!r) ? ret : ret.reverse()
w.value = pre+ret+pos; }
// Novo método para o objeto 'String'
String.prototype.reverse = function(){
return this.split('').reverse().join(''); };
</script>

<script language="javascript">
function number_format( number, decimals, dec_point, thousands_sep ) {
var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
var d = dec_point == undefined ? "," : dec_point;
var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}
</script>

<script> 
function calcula(operacion){ 
var operando1 = parseFloat( document.calc.operando1.value.replace(/\./g, "").replace(",", ".") );
var operando2 = parseFloat( document.calc.operando2.value.replace(/\./g, "").replace(",", ".") );
var result = eval(operando1 + operacion + operando2);
document.calc.resultado.value = number_format(result,2, ',', '.');
</script> 
</head>

<body>

<h1>Java Script - Formatar casas decimais</h1>
<br>
<form name="calc">
<input type="Text" name="operando1" value="" size="12" onKeyUp="maskIt(this,event,'###.###.###,##',true)" dir="rtl"> 
<br/> 
<input type="Text" name="operando2" value="" size="12" onKeyUp="maskIt(this,event,'###.###.###,##',true)" dir="rtl"> 
<br/> 
<input type="Button" name="" value="Somar" onclick="calcula('+')"> 

<br/> 
<input type="Text" name="resultado" value="" size="12" dir="rtl"/> 
<br/>
</form>
</body>
</html>

Nenhum comentário:

Postar um comentário