Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2005
    Posts
    196
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How Can I Multiplying Currency?

    For example, if I have 25,000.00 * 2, how could I get 50,000.00 in return? Also, what if I have cents as my price?

    Thanks

  • #2
    Regular Coder
    Join Date
    Oct 2005
    Posts
    196
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Any ideas?

  • #3
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,802
    Thanks
    19
    Thanked 157 Times in 148 Posts
    it sounds like you are concerned about the manipulation and formatting of currency strings, but your question isn;t posed very well...

    In any event, you want something like this perhaps:

    Code:
     
      <script type="text/javascript">
      function addCommas(nStr)
      {
       nStr += '';
       x = nStr.split('.');
       x1 = x[0];
       x2 = x.length > 1 ? '.' + x[1] : '';
       var rgx = /(\d+)(\d{3})/;
       while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
       }
       return x1 + x2;
      }
      var amountString = "25,000.00";
      amountString = amountString.replace(/,/g,"");
      var num = parseInt(amountString);
      var newNum = num * 2;
      var productString  = addCommas(newNum.toFixed(2));
      alert(productString);
      </script>
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #4
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,802
    Thanks
    19
    Thanked 157 Times in 148 Posts
    a more flexible version, for doing different types of currency arithmetic:

    Code:
     
    <script type="text/javascript">
     
    var operators = new Array("+","-","*","/");
     
    function doCurrencyMath(currencyString,currencyString2,num,operator)
    {
     var currencyNum = parseFloat(currencyString.replace(/,/g,""));
     var newNum;
     if (num == null && currencyString2 != null)
     {
      var currencyNum2 = parseFloat(currencyString2.replace(/,/g,""));
      newNum = getNewNum(operator,currencyNum,currencyNum2)
     }
     else if (currencyString2 == null && num != null)
     {
      newNum = getNewNum(operator,currencyNum,num)
     }
     else
     {
      return "Error!";
     }
     if (newNum == "Error!")
      return "Error!";
     var productString = addCommas(newNum.toFixed(2));
     return productString;
    }
     
    function getNewNum(operator,num1,num2)
    {
     if (operator == "+")
      return num1 + num2;
     else if (operator == "-")
      return num1 - num2;
     else if (operator == "*")
      return num1 * num2;
     else if (operator == "/")
      return num1 / num2;
     else
      return "Error!";
    }
     
    function addCommas(nStr)
    {
       nStr += '';
       x = nStr.split('.');
       x1 = x[0];
       x2 = (x.length > 1) ? '.' + x[1] : '';
       var rgx = /(\d+)(\d{3})/;
       while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
       }
       return x1 + x2;
    }
     
    // Test currency arithmetic
    // Either the second or third parameter of doCurrencyMath() must be null, or an error will be returned.
    // For doCurrencyMath(), the first and second parameters are strings, the third is a number, and the fourth is the operator array member.
     
    // print 25,000.00 * 2
    alert(doCurrencyMath("25,000.00",null,2,operators[2]));
     
    // print 25,000,000.50 - 5
    alert(doCurrencyMath("25,000,000.50",null,5,operators[1]));
     
    // print 0.50 + 15.50
    alert(doCurrencyMath("0.50",null,15.50,operators[0]));
     
    //print 25,000.00 / 12,500.00
    alert(doCurrencyMath("25,000.00","12,500.00",null,operators[3]));
     
    //produce errors
    alert(doCurrencyMath("25,000.00","25,000.00",5,operators[0]));
    alert(doCurrencyMath("25,000.00","25,000.00",null,"="));
    
     
    </script>
    Last edited by chump2877; 09-05-2007 at 06:01 AM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!


  •  

    Posting Permissions

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