Quelltext der Datei: http://www.michaelster.ch/lernen/__new__mathround.htm

Dateigrösse: 7.17 kb

[Anzeige mit Zeilennummern]



    <style>
        h2     { font-family:palatino,calibri,verdana,sans-serif; font-weight:bolder; font-size:20pt !important; margin:0; padding:0 0 0.8em 0; color: #333; letter-spacing: 0.1em; } 
        .content form     { font: 11pt verdana,sans-serif !important; padding:1pt 0; }
        .content p         { padding:5px 0 !important; margin-bottom:1pt; }
        .result, .raw, .runden { font: 11pt verdana,sans-serif !important;  }
        .result     { min-width: 165pt !important; font-size:11pt !important; font-weight:bold !important; padding:8px 4px 8px 8px !important; border:1px solid gray; border-radius:3pt; box-shadow:2pt 2pt 3pt crimson; }
        .raw        { width: 175pt !important; border-radius:3pt; padding:5pt 5pt !important; }
        .runden        { width: 175pt !important; }
        .interg     { margin-top:2em !important; padding:5pt 5pt !important; }

        .result .weiter         { float:right; }
        .result .weiter    a     { font-weight:bold; text-decoration:none; color:#000; padding:0 10px 0 20px; }
        .result .weiter    a:hover { text-decoration:overline; color:crimson; }
    </style>
    
    <script>
      function round(wert, dez) 
      {
        wert = parseFloat(wert);
        if (!wert) return 0;

        dez = parseInt(dez);
        if (!dez) dez=0;

        var umrechnungsfaktor = Math.pow(10, dez);
        var multi = Math.round(wert * umrechnungsfaktor);
        var res   = multi / umrechnungsfaktor;
        /*** Control in der Console ***/
        console.log('###################################');
        console.log(umrechnungsfaktor);
        console.log(multi);
        console.log(dez);
        console.log("Resultat: " + res);
        if(umrechnungsfaktor == '0.00001') {
            res = Math.round(res);
            console.log("Resultat_r: " + res);
        }
        if (typeof res === 'number') {
            text = res.toString();
        }
        const myArray = text.split(".");
        if(myArray[1]==000000001 && dez<1) {
            console.log("Resultat_GZ: " + myArray[1]);
            res = parseInt(myArray[0]);
        }
        console.log('Stellen: '+myArray[0].length);
        console.log((res).toLocaleString('de-ch'));
          if(myArray[0].length >3 && dez<4) {
              return (res).toLocaleString('de-ch');
          } else {
            return res;
          }
      }
      /*** Beispiel ****************************************************
      zahl = 8175.4647693075;
      gerundet = round(zahl, 3); //auf drei Stellen runden = 8175.465
      *****************************************************************/
      function fuckme (wert, dez=8) 
      {
        valeur = wert.replace(',', '.');
        valore = valeur.replace(/'/g, '');
        console.log(valore);
       
        index(valore);
        
        var result = 0;
        result = round(valore, dez);
        console.log("Resultat_final: " + result);
          
        var weiter = '<div class="weiter"><a href="./?nid=eight" title="n&auml;chste Zahl">&raquo;&raquo;&raquo;</a></div>';
        
        if(valore!=result) {
            document.getElementById('resultbox').innerHTML=result;
            document.getElementById('resultbox').innerHTML+=weiter;
            hideres(wert, dez);
        }

      }
      
      function index(valore) 
      {
        var l = valore.length; // alle Dezimalstellen (plus Kommata/Punkt)
        var n = valore.indexOf("."); // Position Komma/Punkt, wenn keiner n = -1
        var k = ((l-n)-2); // Differenz von l und n minus Punkt und letzter Stelle (-2)
        
        //numinfo(l,n,k); 
        
        error(n,l);
        
        if(n != -1) 
        {
          while(k >0) {
            console.log(k);
            document.getElementById(k).style.display='flex';
            k--;
          }
          n--;
          while(n >=0) {
            console.log(n);
            document.getElementById('p'+n).style.display='flex';
            n--;
          }
        } else {
          l--;
          while(l >=0) {
            console.log(l);
            document.getElementById('p'+l).style.display='flex';
            l--;
          }
        }  
    }
    function error(n, l) {
        if((n == -1 && l >7) || (n != -1 && n >7)) {
            errormsg();
        }
        if(((l-n)-1) >7) {
            errormsg2();
        }
    }
    function errormsg() {
        document.write('<html style="padding:0; margin:0;"><body style="padding:2.5em 3em;"><h1 style="font:bold 20pt verdana,sans-serif; color:crimson;">&clubs; Zahl zu gross &diams; max. 9 Millionen &spades;</h1><button  style="padding:5pt 10pt; margin-top:1em; border-radius:5px; font:bold 11pt verdana,sans-serif; border:2pt solid black; background:white;" onclick="location.href=\'http://www.michaelster.ch/lernen/?nid=eight\';">verstanden</button></body></html>'); 
    }
    function errormsg2() {
        document.write('<html style="padding:0; margin:0;"><body style="padding:2.5em 3em;"><h1 style="font:bold 20pt verdana,sans-serif; color:crimson;">&diams; Zu lange Zahl &clubs; max. 7 Dezimalstellen &spades;</h1><button  style="padding:5pt 10pt; margin-top:1em; border-radius:5px; font:bold 11pt verdana,sans-serif; border:2pt solid black; background:white;" onclick="location.href=\'http://www.michaelster.ch/lernen/?nid=eight\';">verstanden</button></body></html>'); 
    }
    function numinfo(l,n,k) {
        document.getElementById("interg").innerHTML = n + " :: " + l + " :: " + k;
    }
    function hideres(wert, dez, valore) {
         if(wert && dez) {
            document.getElementById('interg').style.display='flex';
            document.getElementById('resultbox').style.display='inline-block';
        } else {
            document.getElementById('interg').style.display='none';
            document.getElementById('resultbox').style.display='none';
        }
    }
    
    </script>

    
  <h2>Math.round()</h2>

<div class="content">
  <form name="f0" id="f0">
    <p>Zahl:</p>
    <input type="text" id="zahl" name="rawnumber" placeholder="Bitte hier eingeben" onblur="fuckme(this.value);" title="Kein Komma, nur Punkt" size="15" maxlength="15" class="raw" value="" autofocus />
      <br><br>
    <p>runden auf:</p>
    <select name="selround" class="runden" size="1" id="sel_id" onchange="fuckme(document.getElementById('f0').elements['rawnumber'].value, this.value);">
        <option value="">Bitte w&auml;hlen &nbsp;</option>
        <option value="">- <> - <> - <> - <> - <> - <> - <> - &nbsp;</option>
        <option style="display:none" id="6" value="6">&nbsp;&bull;&nbsp;millionstel</option>
        <option style="display:none" id="5" value="5">&nbsp;&bull;&nbsp;hunderttausendstel</option>
        <option style="display:none" id="4" value="4">&nbsp;&bull;&nbsp;zehntausendstel</option>
        <option style="display:none" id="3" value="3">&nbsp;&bull;&nbsp;tausendstel</option>
        <option style="display:none" id="2" value="2">&nbsp;&bull;&nbsp;hundertstel</option>
        <option style="display:none" id="1" value="1">&nbsp;&bull;&nbsp;zehntel</option>
        <option style="display:none" id="p0" value="0">&nbsp;¤&nbsp; Einer</option>
        <option style="display:none" id="p1" value="-1">&nbsp;¤&nbsp; Zehner</option>
        <option style="display:none" id="p2" value="-2">&nbsp;¤&nbsp; Hunderter</option>
        <option style="display:none" id="p3" value="-3">&nbsp;¤&nbsp; Tausender</option>
        <option style="display:none" id="p4" value="-4">&nbsp;¤&nbsp; Zehntausender</option>
        <option style="display:none" id="p5" value="-5">&nbsp;¤&nbsp; Hunderttausender</option>
        <option style="display:none" id="p6" value="-6">&nbsp;¤&nbsp; Millionen</option>
        <option>&nbsp;</option>
    </select>
    <p style="display:none" id="interg" class="interg">
      <b>Resultat:</b>
    </p>
    <div style="display:none" class="result" id="resultbox"><div class="weiter"> <a href="./?nid=eight" title="n&auml;chste Zahl">&raquo;&raquo;&raquo;</a></div></div>
  </form>
</div>