Results 1 to 8 of 8

Thread: Adding 2 currency values together

  1. #1
    Join Date
    Jul 2008
    Posts
    22
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Adding 2 currency values together

    Hello,

    I'm looking to add 2 currency values together, is this possible using Javascript? So basically I have values stored in 2 variables:

    var price1 = "19,99 EUR"
    var price2 = "9,99 EUR"

    Is there any way to add these together to produce a third variable equalling 29,98 EUR?

    Thanks in advance

  2. #2
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default

    you could just take out the EUR and do it like this:

    Code:
    var price1 = 19.99;
    var price2 = 9.99;
    var total = price1 + price2;
    
    document.write(total + "EUR");
    I would assume that that would work...

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Moshambi is right: numerical values should be represented as Numbers internally. Only convert it to a currency-formatted string when it's time to do output, when you've finished all your calculations.

    Here are some functions for converting back and forth:
    Code:
    function toEuro(n) {
      return n.toFixed(2).replace(/\./, ',') + " EUR";
    }
    
    function fromEuro(s) {
      return parseFloat(s.replace(/,/, '.'));
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Jul 2008
    Posts
    22
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Thanks but the prices are being produced dynamically so I can't change the manually. Here is code I put together, but for some reason its not working. Would anybody be able to have a look and help me find where I'm going wrong?

    Code:
    var price1 = "19,99 EUR";
    
    price1_split = price1.split(' ');
    price1 = price1_split[0];//19,99
    cur = price1_split[1];//EUR
    
    
    var price2 = "9,99 EUR";
    
    price2_split = price2.split(' ');
    price2 = price2_split[0];//19,99
    
    
    price1=price1.replace(',','.');
    price1=parseFloat(price1).toFixed(2);
    price3=price1+price2;
     price3=price3.replace('.',',');
    price3 = price1+price2+' '+cur;
    
    
    
    document.write(price3);

  5. #5
    Join Date
    Sep 2008
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by denhamd2 View Post
    Thanks but the prices are being produced dynamically so I can't change the manually.
    Like the above stated,

    Code:
    function toEuro(n) {
      return n.toFixed(2).replace(/\./, ',') + " EUR";
    }
    
    function fromEuro(s) {
      return parseFloat(s.replace(/,/, '.'));
    }
    Convert it FROM Euro, do the equations, then output it to Euros.

    For example:

    Code:
    var a = fromEuro('5,84');
    var b = fromEuro('96,99');
    
    c = a + b;
    
    c = toEuro(c);

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    // I presume these two are those that are generated dynamically.
    var price1 = "19,99 EUR";
    var price2 = "9,99 EUR";
    
    // And here is the manipulation.
    var price3 = toEuro(fromEuro(price1) + fromEuro(price2));
    It's generally better to avoid document.write(). Use DOM manipulation instead.

    If at all possible, editing the server-side script that is generating these values is vastly preferable to converting them client-side.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <script type="text/javascript">
    
    var pricing = {
    numPrice : function(p){
    return p.replace(/,/, '.').replace(/[^\d\.]/g, '') - 0;
    },
    strPrice : function(p){
    return p.toFixed(2).replace(/\./, ',') + ' EUR';
    },
    addPrice : function(){
    for (var r = 0, i = arguments.length - 1; i > -1; --i)
    r += this.numPrice(arguments[i]);
    return this.strPrice(r);
    }
    };
    
    var price1 = "19,99 EUR";
    var price2 = "9,99 EUR";
    
    var price3 = pricing.addPrice(price1, price2);
    alert(price3);
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I was carefully avoiding doing exactly that because it encourages converting between number and string for every single calculation. This is not good coding practice, as it adds an extra layer of overhead in terms of both complexity and computational cost.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •