View Full Version : Adding 2 currency values together
denhamd2
09-17-2008, 03:05 PM
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
Moshambi
09-17-2008, 03:28 PM
you could just take out the EUR and do it like this:
var price1 = 19.99;
var price2 = 9.99;
var total = price1 + price2;
document.write(total + "EUR");
I would assume that that would work...
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:
function toEuro(n) {
return n.toFixed(2).replace(/\./, ',') + " EUR";
}
function fromEuro(s) {
return parseFloat(s.replace(/,/, '.'));
}
denhamd2
09-17-2008, 04:02 PM
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?
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);
tacticious
09-17-2008, 04:09 PM
Thanks but the prices are being produced dynamically so I can't change the manually.
Like the above stated,
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:
var a = fromEuro('5,84');
var b = fromEuro('96,99');
c = a + b;
c = toEuro(c);
// 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 (http://developer.mozilla.org/en/Gecko_DOM_Reference) instead.
If at all possible, editing the server-side script that is generating these values is vastly preferable to converting them client-side.
jscheuer1
09-17-2008, 04:37 PM
<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>
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.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.