Solved

Math.round instead of .toFixed(2)

Posted on 2003-11-25
10
2,257 Views
Last Modified: 2012-05-04
In this page I want a Math.round option instead of .toFixed(2). This because toFixed need ie5.5 or higher to work.
I tried but no succes.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
  <META HTTP-EQUIV="content-type" CONTENT="text/html;charset=iso-8859-1">
    <META NAME=owner CONTENT=none>
  <TITLE>Test</TITLE>

<SCRIPT LANGUAGE=javascript>
itemLines=new Array();
browserVer=parseFloat( navigator.appVersion );
function totup( num )
{
  if( browserVer <= 2 ) return;
  qty=eval( "document.forms[0].Qty"+num+".value" );
  uct=eval( "document.forms[0].UVal"+num+".value" );
  tot=eval( "document.forms[0].STVal"+num);
  tot.value=parseFloat(qty*uct);
  fulltot();
}
function fulltot()
{
  if( browserVer <= 2 ) return;
  var vtot=0;
  var ptot=0;
  for(i=1;i<=5;i++)
  {
    v=parseFloat( eval( "document.forms[0].STVal"+i+".value") );
    p=eval( "document.forms[0].Desc"+i+".value");
    if( !isNaN( v ) ) vtot+=v;
    if( p != "" ) ptot++;
  }
  document.forms[0].TotVal.value=eval( vtot );
  document.forms[0].NumPcs.value=eval( ptot );
}

</SCRIPT>

<SCRIPT LANGUAGE=javascript><!--
function showbars(){}
//--></SCRIPT>

<script language="Javascript">
<!--
function clearRow(theForm,idx)
{
      theForm.elements["CCod"+idx].value=""
      theForm.elements["Desc"+idx].value=""
      theForm.elements["Qty"+idx].value=""
      theForm.elements["UVal"+idx].value=""
      theForm.elements["STVal"+idx].value=""
      theForm.elements["CtOrg"+idx].value=""

       fulltot()
       
 


}




function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function YY_checkform() { //v4.66
//copyright (c)1998,2002 Yaromat.com
  var args = YY_checkform.arguments; var myDot=true; var myV=''; var myErr='';var addErr=false;var myReq;
  for (var i=1; i<args.length;i=i+4){
    if (args[i+1].charAt(0)=='#'){myReq=true; args[i+1]=args[i+1].substring(1);}else{myReq=false}
    var myObj = MM_findObj(args[i].replace(/\[\d+\]/ig,""));
    myV=myObj.value;
    if (myObj.type=='text'||myObj.type=='password'||myObj.type=='hidden'){
      if (myReq&&myObj.value.length==0){addErr=true}
      if ((myV.length>0)&&(args[i+2]==1)){ //fromto
        var myMa=args[i+1].split('_');if(isNaN(myV)||myV<myMa[0]/1||myV > myMa[1]/1){addErr=true}
      } else if ((myV.length>0)&&(args[i+2]==2)){
          var rx=new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-z]{2,4}$");if(!rx.test(myV))addErr=true;
      } else if ((myV.length>0)&&(args[i+2]==3)){ // date
        var myMa=args[i+1].split("#"); var myAt=myV.match(myMa[0]);
        if(myAt){
          var myD=(myAt[myMa[1]])?myAt[myMa[1]]:1; var myM=myAt[myMa[2]]-1; var myY=myAt[myMa[3]];
          var myDate=new Date(myY,myM,myD);
          if(myDate.getFullYear()!=myY||myDate.getDate()!=myD||myDate.getMonth()!=myM){addErr=true};
        }else{addErr=true}
      } else if ((myV.length>0)&&(args[i+2]==4)){ // time
        var myMa=args[i+1].split("#"); var myAt=myV.match(myMa[0]);if(!myAt){addErr=true}
      } else if (myV.length>0&&args[i+2]==5){ // check this 2
            var myObj1 = MM_findObj(args[i+1].replace(/\[\d+\]/ig,""));
            if(myObj1.length)myObj1=myObj1[args[i+1].replace(/(.*\[)|(\].*)/ig,"")];
            if(!myObj1.checked){addErr=true}
      } else if (myV.length>0&&args[i+2]==6){ // the same
            var myObj1 = MM_findObj(args[i+1]);
            if(myV!=myObj1.value){addErr=true}
      }
    } else
    if (!myObj.type&&myObj.length>0&&myObj[0].type=='radio'){
          var myTest = args[i].match(/(.*)\[(\d+)\].*/i);
          var myObj1=(myObj.length>1)?myObj[myTest[2]]:myObj;
      if (args[i+2]==1&&myObj1&&myObj1.checked&&MM_findObj(args[i+1]).value.length/1==0){addErr=true}
      if (args[i+2]==2){
        var myDot=false;
        for(var j=0;j<myObj.length;j++){myDot=myDot||myObj[j].checked}
        if(!myDot){myErr+='* ' +args[i+3]+'\n'}
      }
    } else if (myObj.type=='checkbox'){
      if(args[i+2]==1&&myObj.checked==false){addErr=true}
      if(args[i+2]==2&&myObj.checked&&MM_findObj(args[i+1]).value.length/1==0){addErr=true}
    } else if (myObj.type=='select-one'||myObj.type=='select-multiple'){
      if(args[i+2]==1&&myObj.selectedIndex/1==0){addErr=true}
    }else if (myObj.type=='textarea'){
      if(myV.length<args[i+1]){addErr=true}
    }
    if (addErr){myErr+='* '+args[i+3]+'\n'; addErr=false}
  }
  if (myErr!=''){alert('U heeft de factuur nog niet volledig ingevuld:\t\t\t\t\t\n\n'+myErr)}
  document.MM_returnValue = (myErr=='');
}
//-->
</script>
<SCRIPT>
function radioCheckboxGroup (checkbox) {
  var checkboxGroup = checkbox.form[checkbox.name];
  for (var c = 0; c < checkboxGroup.length; c++)
    if (checkboxGroup[c] != checkbox)
      checkboxGroup[c].checked = false;
  return checkbox.checked;
}
</SCRIPT>
<style type="text/css">
<!--
h1 {  font-family: Arial, Helvetica, sans-serif; font-size: 14px; color: #FFFFFF};
td {  font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #000000};
input { border-style: solid; border-width: 1; font-size: 8pt;};
select { border-style: solid; border-width: 1; font-size: 8pt;};
textarea { border-style: solid; border-width: 1; font-size: 8pt;}
INPUT.BUTTON { font-family: arial, helvetica; }
.custom {
    font-family: Arial, sans, Helvetica;
    font-size: 9pt;
    color: #000000;
    border: solid #000000 0px;
}
-->
</style>
  <STYLE TYPE="text/css"><!--


--></STYLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" LINK="#000000" VLINK="#000000" ALINK="#000077">
<div id="overDiv" style="position:absolute; visibility:hide; z-index:1;">
</div>

<P ALIGN=center><br>
  <br> <FORM NAME=proform>
<CENTER>
    <TABLE BORDER=0 width="549">
      <TR>
        <TD COLSPAN=2>
          <TABLE BORDER=0 WIDTH="100%" CELLSPACING=0>
            <TR align="center">
              <TD width="20%"><font size="1" face="arial,helvetica">Goods</font><font color="#CC0000"></font></TD>
              <TD width="16%"><font size="1" face="arial,helvetica">Douane<br>
                </font></TD>
              <TD width="27%"><font size="1" face="arial,helvetica">Country</font></TD>
              <TD width="6%"><font size=2 face="arial,helvetica"><font size="1">piece</font><font color="#CC0000"></font></font></TD>
              <TD width="11%"><font size=2 face="arial,helvetica"><font size="1">price<br>
                </font></font></TD>
              <TD width="11%"><font size=2 face="arial,helvetica"><font size="1">Total</font></font></TD>
              <TD width="9%">&nbsp;</TD>
            </TR>
            <TR align="center">
              <TD width="20%">
                <input type=TEXT size=17 name=Desc1 onFocus="this.select();return 1;" onChange="fulltot();">
              </TD>
              <TD width="16%">
                <input type=TEXT size=8 name=CCod1 onFocus="this.select();return 1;">
              </TD>
              <TD width="27%">
                <input type="text" name="CtOrg1" size="13">
              </TD>
              <TD width="10%">
                <INPUT TYPE=TEXT SIZE=2 NAME=Qty1 onChange="totup(1)" onFocus="this.select();return 1;">
              </TD>
              <TD width="11%">
                <input type=TEXT size=6 name=UVal1 onChange="totup(1)" onFocus="this.select();return 1;">
              </TD>
              <TD width="11%">
                <INPUT TYPE=TEXT SIZE=6 NAME=STVal1 onFocus="this.select();return 1;" readonly>
              </TD>
              <TD width="9%">
                <input class="custom" name="image" type="image" onClick="clearRow(this.form,1); return false" src="../pics/del.gif" width="12" height="12">
              </TD>
            </TR>
            <TR align="center">
              <TD width="20%">
                <input type=TEXT size=17 name=Desc2 onFocus="this.select();return 1;" onChange="fulltot();">
              </TD>
              <TD width="16%">
                <input type=TEXT size=8 name=CCod2 onFocus="this.select();return 1;">
              </TD>
              <TD width="27%">
                <input type="text" name="CtOrg2" size="13">
              </TD>
              <TD width="10%">
                <INPUT TYPE=TEXT SIZE=2 NAME=Qty2 onChange="totup(2)" onFocus="this.select();return 1;">
              </TD>
              <TD width="11%">
                <INPUT TYPE=TEXT SIZE=6 NAME=UVal2 onChange="totup(2)" onFocus="this.select();return 1;">
              </TD>
              <TD width="11%">
                <INPUT TYPE=TEXT SIZE=6 NAME=STVal2 onFocus="this.select();return 1;" readonly>
              </TD>
              <TD width="9%">
                <input class="custom" name="image2" type="image" onClick="clearRow(this.form,2); return false" src="../pics/del.gif" width="12" height="12">
              </TD>
            </TR>
            <TR align="center">
              <TD width="20%">
                <input type=TEXT size=17 name=Desc3 onFocus="this.select();return 1;" onChange="fulltot();">
              </TD>
              <TD width="16%">
                <input type=TEXT size=8 name=CCod3 onFocus="this.select();return 1;">
              </TD>
              <TD width="27%">
                <input type="text" name="CtOrg3" size="13">
              </TD>
              <TD width="10%">
                <INPUT TYPE=TEXT SIZE=2 NAME=Qty3 onChange="totup(3)" onFocus="this.select();return 1;">
              </TD>
              <TD width="11%">
                <input type=TEXT size=6 name=UVal3 onChange="totup(3)" onFocus="this.select();return 1;">
              </TD>
              <TD width="11%">
                <INPUT TYPE=TEXT SIZE=6 NAME=STVal3 onFocus="this.select();return 1;" readonly>
              </TD>
              <TD width="9%">
                <input class="custom" name="image" type="image" onClick="clearRow(this.form,3); return false" src="../pics/del.gif" width="12" height="12">
              </TD>
            </TR>
            <TR align="center">
              <TD width="20%">
                <input type=TEXT size=17 name=Desc4 onFocus="this.select();return 1;" onChange="fulltot();">
              </TD>
              <TD width="16%">
                <input type=TEXT size=8 name=CCod4 onFocus="this.select();return 1;">
              </TD>
              <TD width="27%">
                <input type="text" name="CtOrg4" size="13">
              </TD>
              <TD width="10%">
                <INPUT TYPE=TEXT SIZE=2 NAME=Qty4 onChange="totup(4)" onFocus="this.select();return 1;">
              </TD>
              <TD width="11%">
                <input type=TEXT size=6 name=UVal4 onChange="totup(4)" onFocus="this.select();return 1;">
              </TD>
              <TD width="11%">
                <INPUT TYPE=TEXT SIZE=6 NAME=STVal4 onFocus="this.select();return 1;" readonly>
              </TD>
              <TD width="9%">
                <input class="custom" name="image" type="image" onClick="clearRow(this.form,4); return false" src="../pics/del.gif" width="12" height="12">
              </TD>
            </TR>
            <TR align="center">
              <TD width="20%">
                <input type=TEXT size=17 name=Desc5 onFocus="this.select();return 1;" onChange="fulltot();">
              </TD>
              <TD width="16%">
                <input type=TEXT size=8 name=CCod5 onFocus="this.select();return 1;">
              </TD>
              <TD width="27%">
                <input type="text" name="CtOrg5" size="13">
              </TD>
              <TD width="10%">
                <INPUT TYPE=TEXT SIZE=2 NAME=Qty5 onChange="totup(5)" onFocus="this.select();return 1;">
              </TD>
              <TD width="11%">
                <INPUT TYPE=TEXT SIZE=6 NAME=UVal5 onChange="totup(5)" onFocus="this.select();return 1;">
              </TD>
              <TD width="11%">
                <INPUT TYPE=TEXT SIZE=6 NAME=STVal5 onFocus="this.select();return 1;" readonly>
              </TD>
              <TD width="9%">
                <input class="custom" name="image" type="image" onClick="clearRow(this.form,5); return false" src="../pics/del.gif" width="12" height="12">
              </TD>
            </TR>
          </TABLE>
</TD>
      </TR>
      <TR>
        <TD COLSPAN=2>
          <HR SIZE=1>
        </TD>
      </TR>
      <TR>
        <TD COLSPAN=2>
          <table border=0 width="100%" cellspacing=0>
            <tr valign="middle">
              <td width="26%"><font size=2 face="arial,helvetica"> Total goods</font></td>
              <td width="18%">
                <input type=text value=0 size=10 name=NumPcs readonly>
              </td>
              <td width="19%">Valuta code</td>
              <td width="37%"><font face="arial,helvetica" size=2>
                <input type=text name=Currncy size=4 maxlength=3 value="EUR">
                </font></td>
            </tr>
            <tr valign=middle>
              <td width="26%"><font size=2 face="arial,helvetica"> <b>Total weight</b></font></td>
              <td width="18%">
                <input type=text value=0 size=10 name=NetWgt>
              </td>
              <td width="19%">Transportcost</td>
              <td width="37%"><font face="arial,helvetica" size=2>
                <input type=text name="FrChr" size=10 onFocus="this.select();return 1;" value="0">
                </font></td>
            </tr>
            <tr valign="middle">
              <td width="26%"><font size=2 face="arial,helvetica"> total value</font></td>
              <td width="18%">
                <input type=text value=0 size=10 name=TotVal readonly>
              </td>
              <td width="19%">&nbsp;</td>
              <td width="37%"><font face="arial,helvetica" size=2>&nbsp; </font></td>
            </tr>
          </table>
        </TD>
      </TR>
      <TR>
        <TD COLSPAN=2>
          <HR SIZE=1>
        </TD>
      </TR>
     
      <TR>
        <TD align="center" colspan="2">&nbsp;</TD>
      </TR>
      <TR>
        <TD align="center"><FONT SIZE=2>
          <INPUT TYPE=submit CLASS=BUTTON onClick="return document.MM_returnValue" VALUE="Print Invoice">
          </FONT></TD>
        <TD align="center"><FONT SIZE=2>
          <INPUT CLASS=BUTTON TYPE=reset VALUE="Clear Form">
          </FONT></TD>
      </TR>
    </TABLE>
</CENTER>
</form>
<p>
 
</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</BODY></HTML>
0
Comment
Question by:Milcoi
10 Comments
 

Author Comment

by:Milcoi
Comment Utility
But always keep 2 decimals bihind the dot even when it is (for example) 54.00
0
 
LVL 8

Expert Comment

by:xabi
Comment Utility
Hi Milcoi,

Try this:

Math.round(eval( vtot ) * 100) / 100;

If you want 3 decimals ... just change 100 by 1000

xabi
0
 
LVL 8

Expert Comment

by:xabi
Comment Utility
 var sValue = Math.round(eval( vtot ) * 100) + "";
  sValue = sValue.substr(0, sValue.length-2) + "." + sValue.substr(sValue.length-2)
  document.forms[0].TotVal.value=sValue
0
 

Author Comment

by:Milcoi
Comment Utility
Ok but where to put this in the script?
0
 

Author Comment

by:Milcoi
Comment Utility
I realy need 2 decimals behind the dot.

When you fill in 6 pieces and price 8.90 or 9.80 you will see
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 8

Accepted Solution

by:
xabi earned 85 total points
Comment Utility
Use this function:

function giveNumber (iValue) {
  var sValue = Math.round(iValue * 100) + "";
  var sPart1 = sValue.substr(0, sValue.length-2);
  var sPart2 = sValue.substr(sValue.length-2);
  return (sPart1 == "" ? "0" : sPart1) + "." + (sPart2 == "0" ? "00" : sPart2);
}

This Way:

document.forms[0].TotVal.value=giveNumber(eval( vtot ));

Xabi
0
 

Author Comment

by:Milcoi
Comment Utility
Also the "total" need to math.round and keep 2 decimals.
0
 
LVL 8

Expert Comment

by:xabi
Comment Utility
tot.value=giveNumber(qty*uct);

Use teh function giveNumber everywhere wyou want 2 decimals.

xabi
0
 
LVL 3

Expert Comment

by:etain
Comment Utility
num - number to convert
Digits - number of Digits after the dot
      
function toDecimal(num, Digits)
{           
      temp = ""
      if (!isNaN(num)) return;
      if (Digits > 0)
      {
            n=Math.round(num*Math.pow(10, Digits))
            for (i = 0 ; i < Digits ; i++)
            {
                  d = n%10
                  n = (n-d)/10
                  temp = (d+"")+temp
            }
            temp = "." + temp
      }
      else n = num

// if u need "," eg: 123,465.78
/*
      while (n > 999)
      {
            temp1 = ""  
            for (i = 0 ; i < 3 ; i++)                  
            {
                  d =  Math.floor(n)%10
                  n =  Math.floor((n-d)/10)
                  temp1 = (d +"")+temp1
            }
            
            temp = ","+ temp1 + temp;
         }
*/
          return  n + temp
}
0
 
LVL 31

Expert Comment

by:GwynforWeb
Comment Utility
what you you mean by

"This because toFixed need ie 5.5 or higher to work."

that is would you would expect of round round(5.5) ie 0.5 and higher goe up lower and goes down

do you want 0.016 -> 0.02 and 0.015->0.02 and 0.014->0.02 ?

I can do this but what do you want?
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now