SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Numbers in a form

    Another quick question. I have the script to add each field up I need, but if the ending 0 after a decimal is present it doesnt show. How do I keep the leading zero and how do I keep just 2 digits after the decimal?

    For example:

    1 + 1.50 = 2.50

    But javascript displays it as 2.5

    Now sometimes when I do math I get:

    2.3454545

    In this case I just want 2.34. So if there is a ending zero I want to keep it, and if there is more than 2 numbers after the decimal I want to cut the rest off and leave the 2 there. Any suggestions?

    Thanks
    Silly

  2. #2
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would like to know the answer to this one aswell? Where is beetle when you need him? We need some sort of a signal like batman had!

  3. #3
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This may be more than you asked for; but what the hay, I can't spend that much time recoding things I already wrote:


    Code:
    <script type="text/javascript">
    <!--
    function num2dollar(fldObj) 
    { 
       var tmp1 = cents = dollars = "";
       var dec = -1;
       var num = i = 0;
       num = fldObj.value;
       num = "" + (Math.round(num * 100)/100);
       dec = num.indexOf(".");
       cents = ((dec > 0) ? num.substring(dec,num.length) : ".00");
       if (cents.length == 2) cents += "0";
       dollars = "" + parseInt(num);
       tmp1 = insComma(dollars);
       num = "$";
       for (i = tmp1.length-1; i >= 0; i--) 
    	  num += tmp1.charAt(i);;
       num +=  cents;
       fldObj.value =  num ;
       
       return(true);
    }
    function insComma(data)
    {
       var count = i = 0;
       var tmpStr = "";
       var comma = ",";
       for (i = data.length-1; i >= 0; i--) 
       {
    	  if (count == 3)
    	  {
    		 tmpStr += comma;
    		 count = 1;
    	  }
    	  else  count ++;
    	  tmpStr += data.charAt(i);
       }
       return(tmpStr);
    }
    //-->
    </script>
    </head>
    <body>
    <form name="a">
    <input type="text" onblur="num2dollar(this)">
    </form>
    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  4. #4
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have done some research into this subject and found a few interesting details! There are three Number object methods that lets a script control the formatting of numbers for displaying as text strings! Each method has a unique purpose, and all return a string formmat. Only after all math calculations have been done on the values should you use these methods! The three methods are as follows:

    number.toExponential(fractionDigits);

    The toExponential() method forces a number to display in exponential notation, even if the number is in the range in which JavaScript normally uses standard notation. The parameter is an integer specifiying how many digits to the right of the decimal are returned, even if they are zeros!

    number.toFixed(fractionDigits);

    Use the toFixed() method when you want to format a number with a specific number of digits displayed to the right of the decimal! This is the method you use to display the results of some sort if calculations using hundredths of units(IE.. dolors and cents). The parameter to the method is an integer indicating the number of digits to be formatted to the right of the decimal. If the number being formatted has more numbers to the right of the decimal than the number of digits specified in the parameter, the method rounds the number to the rightmost visible digit. But only with respect to the unrounded value of the next digit.

    number.toPrecision(precisionDigits);

    The third method is toPercision(), which enables you to define how many total digits(including digits on both the left and right of the decimal) to display of a number. In other words, you define the percision of a number.

    Jon
    Last edited by Poop_Shoot; Sep 1, 2003 at 16:49. Reason: Spelling error in Persicion!

  5. #5
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Jon,
    I have done some research
    Where?
    Have you tried any of those 'methods'? When I tried 'toPercision' all I got was an 'object does not support this method' error message.

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  6. #6
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh Sorry I just did some reading in a book of mine! I have not done any testing! I read that it was both IE5.5+ and NS6+ compatible! Sorry I will do some testing with it after I get off my stupid job!

    Jon

  7. #7
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is odd sometimes they seem to work and other times they do not! I am confused, also when they do work they return odd values like theses?

    1E+2

  8. #8
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AHH H AHAHHAHA


    This is driving me batty WTF! Where is the human javascript archive when you need them? Help?

    Code:
    <script>
    function addMyValue(val_1,val_2){
    var d=document.theFrm;
    var x=d.oper.options[d.oper.selectedIndex].value;
    var val1=parseFloat(val_1);
    var val2=parseFloat(val_2);
    var aws=eval(val1+x+val2);
    var txt=parseFloat(aws);
    var txt1=txt.toExponential(7);
    d.ans1.value=txt;
    
    
    }
    </script>
    
    <form name="theFrm">
    <input type="text" name="val1"> 
    <select name="oper">
    <option value="*">*
    <option value="-">-
    <option value="+">+
    <option value="%">%
    <option value="/">/
    </select>
    <input type="text" name="val2" onblur="addMyValue(this.form.val1.value,this.form.val2.value)"> =
    <input type="text" name="ans1" onfocus="addMyValue(this.form.val1.value,this.form.val2.value)">
    
    
    </form>

  9. #9
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok an answer as been found! I had put toPercision() it should be toPrecision()! My mistake!

    Hey Vinnie, This should work fine now!
    Code:
    <html>
    
    <head>
    
    <title>Numbers</title>
    
    <script type="text/javascript"><!--
    
    function tf(){alert(parseFloat(document.getElementById("num").value).toFixed(5));}
    
    function pf(){alert(parseFloat(document.getElementById("num").value));}
    
    function tp(){alert(parseFloat(document.getElementById("num").value).toPrecision(5));}
    -->
    </script>
    
    </head>
    
    <body>
    
    <form>
    
    <input type="text" id="num" value="145.4540"><br><br><hr><br>
    
    <input type="button" onclick="tf();" value="toFixed"><br><br>
    
    <input type="button" onclick="pf();" value="parseFloat"><br><br>
    
    <input type="button" onclick="tp();" value="toPrecision"><br>
    
    </form>
    
    </body>
    
    </html>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •