Webskater
07-09-2003, 08:46 AM
I have two fields with say £25.00 in one and £20.00 in the other. Can I add them together without having to strip the currency symbol away. Thanks for any help.
|
Click to See Complete Forum and Search --> : Adding currency Webskater 07-09-2003, 08:46 AM I have two fields with say £25.00 in one and £20.00 in the other. Can I add them together without having to strip the currency symbol away. Thanks for any help. Fang 07-09-2003, 08:52 AM No. Khalid Ali 07-09-2003, 08:54 AM not javascript will only consider them strings therefore "+" (plus) operator will concatenate both strings and the result will be this £25.00£20.00 to make javascript add numbers youll need to strip pound sign and add the numbers and put the number sign again.. Webskater 07-09-2003, 08:57 AM Thanks for your replies. Charles 07-09-2003, 02:17 PM Here's something that can be adapted to work on your side of the puddle. It defines a class of Dollars objects that have the nifty property that if you use them like numbers they behave like numbers but if you use them as strings they look like currency. <script type="text/javascript"> <!-- String.prototype.reverse = function () {return this.split('').reverse().join('')}; function Dollars (d) {this.ammount = typeof d == 'number' ? d : Number(d.toString().replace(/[$,]/g, ''))}; Dollars.prototype.valueOf = function () {return this.ammount}; Dollars.prototype.toString = function () { if (isNaN (this.ammount)) return NaN.toString(); var l = Math.floor(Math.abs(this.ammount)).toString(); var r = Math.round((Math.abs(this.ammount) % 1) * 100).toString(); return [(this.ammount < 0 ? '-' : ''), '$', (l.length > 4 ? l.reverse().match(/\d{1,3}/g).join(',').reverse() : l),'.', (r < 10 ? '0' + r : r)].join(''); } // --> </script> webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |