www.webdeveloper.com
Results 1 to 9 of 9

Thread: Displaying javascript variables in currency form

  1. #1
    Join Date
    Sep 2004
    Posts
    7

    Displaying javascript variables in currency form

    I'm trying to format the output of my script so that the everything is in currency format. Here's my code:

    Code:
    <script language="javascript">
    threeblue = unescape(params["threeblue"]);
    threepink = unescape(params["threepink"]);
    thirdblue = unescape(params["thirdblue"]);
    thirdpink = unescape(params["thirdpink"]);
    chargetotal=(threeblue*8.95) + (threepink*8.95) + (thirdblue*1.75) + (thirdpink*1.75);
    if(threeblue != 0)
    {
    document.write("<tr><td>" + "3 Oz Blue Bottle of Happy Hiney" + "</td><td><center>" + threeblue  + "</center></td></tr>");
    }
    if(threepink != 0)
    {
    document.write("<tr><td>" + "3 Oz Pink Bottle of Happy Hiney" + "</td><td><center>" + threepink  + "</center></td></tr>");
    }
    if(thirdblue !=0)
    {
    document.write("<tr><td>" + "1/3 Oz Blue Bottle of Happy Hiney" + "</td><td><center>" + thirdblue  + "</center></td></tr>");
    }
    if(thirdpink != 0)
    {
    document.write("<tr><td>" + "1/3 Oz Pink Bottle of Happy Hiney" + "</td><td><center>" + thirdpink  + "</center></td></tr>");
    }
    
    document.write("<tr><td><center>" + "<b>Your Total:</b>" + "$ " + chargetotal + "</center><br>");
                    </script>
    When writing the chargetotal, I want it to always have two decimal places. Any ideas?

  2. #2
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    You could use the toFixed() method, however that only works in modern browsers. I've written three custom functions to do the same thing -- the fastest of which, I'm not sure as they are all relatively quick.

    Code:
    <script type="text/javascript"><!--
      Number.prototype.to_Fix = function(n){
       var str = this.toString();
       var newStr = str.split(".")[0], xStr = "", oStr   = str.split(".")[1];
        for(var i=0; i<n; ++i){xStr += (typeof oStr[i]=='undefined')?'0':oStr[i];}
       return newStr+"."+xStr;
      }
    
      Number.prototype.to_Fix_sub = function(n){
       var str = this.toString();
           str = str.split(".")[0]+str.substr(str.indexOf("."), n+1);
         for(i=0; i<n; i++){if(typeof(str.split(".")[1][i])=="undefined"){str += "0";}}
        return str;
      }
      
      Number.prototype.to_Fix_RE = function(n){
       var str = this.toString();
         str = str.replace(eval("/(\\d+\\.)(\\d{"+n+"})\\d*/"), "$1$2");
         for(i=0; i<n; i++){if(typeof(str.split(".")[1][i])=="undefined"){str += "0";}}
      return str;
      }
    //--></script>
    This is how you'd use the functions.

    Code:
    alert(changeTotal.to_Fix(2));
    alert(changeTotal.to_Fix_sub(2));
    alert(changeTotal.to_Fix_RE(2));

  3. #3
    Join Date
    Sep 2004
    Posts
    7
    In the first function it just displays two zeros and not the actual decimal value. If total was 8.95, it displays 8.00

    The other two functions display the corrent math, but give an extra zero, so it is fixed to three decimal places. I've tried playing with the code to make it only display two places, but no progress. Any idea on how to fix this?

  4. #4
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    What browser/version are you using? This works perfectly for me.

    Code:
      var chargeTotal = 2.02*1.75;
       alert(chargeTotal);
       alert(chargeTotal.to_Fix(2));
       alert(chargeTotal.to_Fix_sub(2));
       alert(chargeTotal.to_Fix_RE(2));

  5. #5
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    I've discovered the issue was with Internet Explorer not treating each char of a string as a seperate value (retrieved with an array index). I've replaced it with the charAt() method, and it works in all browsers I've tested it in now.

    Code:
      Number.prototype.to_Fix = function(n){
       var str = this.toString();
       var newStr = str.split(".")[0], xStr = "", oStr   = str.split(".")[1], temp;
        for(var i=0; i<n; ++i){
    	  xStr += (typeof oStr.charAt(i)=='undefined')?'0':oStr.charAt(i);
    	}
       return newStr+"."+xStr;
      }
    Edit: I've also fixed the other two functions. Could you please tell me which works most efficiently? Thanks.

    Code:
      Number.prototype.to_Fix_sub = function(n){
       var str = this.toString();
           str = str.split(".")[0]+str.substr(str.indexOf("."), n+1);
         for(i=0; i<n; i++){if(typeof(str.split(".")[1].charAt(i))=="undefined"){str += "0";}}
        return str;
      }
      
      Number.prototype.to_Fix_RE = function(n){
       var str = this.toString();
         str = str.replace(eval("/(\\d+\\.)(\\d{"+n+"})\\d*/"), "$1$2");
         for(i=0; i<n; i++){if(typeof(str.split(".")[1].charAt(i))=="undefined"){str += "0";}}
      return str;
      }
    Last edited by Jona; 09-10-2004 at 03:48 PM.

  6. #6
    Join Date
    Sep 2004
    Posts
    7
    Now all three of the scripts do not work. They are not displaying the zeros. If the chargetotal is 17.90, it displays 17.9. I tested it with IE 6 and Firefox and they both show the same error. I really appreciate all of the help!

  7. #7
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    What error?

  8. #8
    Join Date
    Sep 2004
    Posts
    7
    It's not exactly showing an error, the scripts just don't work. Like I said above, if the total was 17.90, it only displays 17.9. All three of the scripts do this. I really appreciate your patience and help!

  9. #9
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    What browser/version are you using, and what code are you using?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles