Gernot Back: Preise formatieren

Beitrag lesen

Hallo nochmal,

was ich hier selbst zu dem Thema im Archiv fand (ja da gab es durchaus etwas!) hat mir nicht so gefallen; deshalb habe ich mal selbst was gebastelt:

  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
<html>  
<head>  
<title>Preis formatieren</title>  
<meta name="author" content="Gernot Back" />  
<meta name="generator" content="Ulli Meybohms HTML EDITOR" />  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
<style type="text/css">  
[code lang=css]  
td, td input {  
  font-family:sans-serif;  
  white-space:nowrap;  
  font-size:.9em;  
  padding:0;  
  margin0;  
}  
  
td.inp input {  
  font-family:"Courier New", Courier, monospace;  
  text-align:right;  
  float:right;  
  padding:0 .3em;  
  border:solid 1px black;  
  width:15.5em;  
}

</style>
<script type="text/javascript">

  
function format (obj) {  
  var zahl = parseFloat(obj.inp.value);  
  var dsep = obj.dsep.value;  
  if (dsep!=",") dsep =".";  
  obj.dsep.value = dsep;  
  var tsep = obj.tsep.value;  
  if (tsep==dsep||(tsep!=','&&tsep!='.'&&tsep!=' ')) {  
    if(dsep=='.')  
      tsep=",";  
    else  
      tsep=" ";  
    obj.tsep.value=tsep;  
  }  
  var prec = parseInt(obj.prec.value);  
  var bloecke=new Array();  
  if (prec>0)  
    var nkomma = (zahl%1).toFixed(prec);  
  else  
    var nkomma = '';  
  nkomma = nkomma.replace(/0\./,dsep)  
  zahl=parseInt(zahl);  
  for (i=0; zahl>0; i++) {  
      bloecke[i]=zahl%1000;  
      zahl/=1000;  
      zahl=parseInt(zahl);  
  }  
  zahl=bloecke.reverse().join(tsep);  
  obj.outp.value = zahl + nkomma + ' EUR';  
  return false;  
}

</script>
</head>
<body>
<h1>Formatierung von Preisen</h1>
<form action="#" onsubmit="~~~javascript

return format(this)

<table>  
<tr>  
 <td>Eingabe:</td>  
 <td class="inp"><input type="text" name="inp" value="123456.789" size="10" maxlength="10" /></td>  
 <td>Zahl in amerikanischem Format</td>  
</tr>  
<tr>  
 <td>Dezimaltrenner:</td>  
 <td class="inp"><input class="sep" type="text" name="dsep" value="," size="1" maxlength="1" /></td>  
 <td>Komma oder Punkt</td>  
</tr>  
<tr>  
 <td>Nachkommastellen:</td>  
 <td class="inp"><input type="text" name="prec" value="2" size="1" maxlength="1" /></td>  
 <td>Null bis zu neun</td>  
</tr>  
<tr>  
 <td>Tausendertrenner:</td>  
 <td class="inp"><input class="sep" type="text" name="tsep" value="." size="1" maxlength="1" /></td>  
 <td>Komma, Punkt oder Leerzeichen</td>  
</tr>  
<tr>  
 <td><input type="submit" name="" value="Ausgabe" /></td>  
 <td class="inp"><input type="text" name="outp" value="" size="" maxlength="" readonly="readonly" /></td>  
 <td>Feld nicht von Hand editierbar</td>  
</tr>  
</table>  
</form>  
</body>  
</html>[/code]  
  
Gruß Gernot