Documentation

Calculating with currencies - Zend_Currency

Calculating with currencies

When working with currencies you will sometimes also have to calculate with them. Zend_Currency allows you to do this with some simple methods. The following methods are supported for calculation:

  • add(): This method adds the given currency to the existing currency object.

  • sub(): This method substracts the given currency from the existing currency object.

  • div(): This method divides the given currency from the existing currency object.

  • mul(): This method multiplies the given currency with the existing currency object.

  • mod(): This method calculates the remaining value (modulo) from dividing the given currency from the existing currency object.

  • compare(): This method compares the given currency with the existing currency object. When both values are equal it returns '0'. When the existing currency value is greater than the given, this method will return 1. Otherwise you will get '-1' returned.

  • equals(): This method compares the given currency with the existing currency object. When both values are equal it returns TRUE, otherwise FALSE.

  • isMore(): This method compares the given currency with the existing currency object. When the existing currency is greater than the given one, you will get TRUE in return, otherwise FALSE.

  • isLess(): This method compares the given currency with the existing currency object. When the existing currency is less than the given one, you will get TRUE in return, otherwise FALSE.

As you can see the multiple methods allow any kind of calculation with Zend_Currency. See the next snippets as example:

  1. $currency = new Zend_Currency(
  2.     array(
  3.         'value'    => 1000,
  4.         'currency' => 'USD',
  5.     )
  6. );
  7.  
  8. print $currency; // Could return '$ 1.000,00'
  9.  
  10. $currency->add(500);
  11. print $currency; // Could return '$ 1.500,00'
  1. $currency_2 = new Zend_Currency(
  2.     array(
  3.         'value'    => 500,
  4.         'currency' => 'USD',
  5.     )
  6. );
  7.  
  8. if ($currency->isMore($currency_2)) {
  9.     print "First is more";
  10. }
  11.  
  12. $currency->div(5);
  13. print $currency; // Could return '$ 200,00'

Copyright

© 2006-2021 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts