Contributions

Features (Category Index)
Search: 

Get Numeric Value of a Currency Price

Working with multiple currencies is great, until you need to do things with the actual numbers like add/subtract/multiply. Then you're in trouble, because the prices are generally stored in variables as "$14.99" and "14.99EUR" -- with the symbol INCLUDED, which prevents math operations. If you're only using one currency, you can simply use the database value for price, but you can't do this if you plan on using more than one currency.

Because of this, I added a new function to the currencies class: get_numeric_value(). With this, you can pass any formatted currency string to the function and it will return just the number--no matter what currency is being used! So if a customer is browsing with dollars as her currency, the string "$14.99" will pop back as "14.99", ready to be used for math. (Useful if you need math to calculate "you save xx percent" and such.) And another customer browsing the same item for "11.99EUR" will have the script pop back "11.99". Cool.

Two files need to be altered to enable this new $currencies method:

catalog/includes/classes/currencies.php
catalog/admin/includes/classes/currencies.php

The good part is you can make this modification, and never even use it, and it won't affect you at all; it's just an optional new function you can call.

Difficulty: unbelievably easy

Expand All / Collapse All

Get Numeric Value of a Currency Price Matt Friedman 26 Sep 2003

Working with multiple currencies is great, until you need to do things with the actual numbers like add/subtract/multiply. Then you're in trouble, because the prices are generally stored in variables as "$14.99" and "14.99EUR" -- with the symbol INCLUDED, which prevents math operations. If you're only using one currency, you can simply use the database value for price, but you can't do this if you plan on using more than one currency.

Because of this, I added a new function to the currencies class: get_numeric_value(). With this, you can pass any formatted currency string to the function and it will return just the number--no matter what currency is being used! So if a customer is browsing with dollars as her currency, the string "$14.99" will pop back as "14.99", ready to be used for math. (Useful if you need math to calculate "you save xx percent" and such.) And another customer browsing the same item for "11.99EUR" will have the script pop back "11.99". Cool.

Two files need to be altered to enable this new $currencies method:

catalog/includes/classes/currencies.php
catalog/admin/includes/classes/currencies.php

The good part is you can make this modification, and never even use it, and it won't affect you at all; it's just an optional new function you can call.

Difficulty: unbelievably easy

Note: Contributions are used at own risk.