Zend_Currency-Calculation.xml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.currency.calculation">
  4. <title>Calculating with currencies</title>
  5. <para>
  6. When working with currencies you will sometimes also have to calculate with them.
  7. <classname>Zend_Currency</classname> allows you to do this with some simple methods.
  8. The following methods are supported for calculation:
  9. </para>
  10. <itemizedlist mark='opencircle'>
  11. <listitem>
  12. <para>
  13. <emphasis><methodname>add()</methodname></emphasis>: This method adds the given
  14. currency to the existing currency object.
  15. </para>
  16. </listitem>
  17. <listitem>
  18. <para>
  19. <emphasis><methodname>sub()</methodname></emphasis>: This method substracts the
  20. given currency from the existing currency object.
  21. </para>
  22. </listitem>
  23. <listitem>
  24. <para>
  25. <emphasis><methodname>div()</methodname></emphasis>: This method divides the
  26. given currency from the existing currency object.
  27. </para>
  28. </listitem>
  29. <listitem>
  30. <para>
  31. <emphasis><methodname>mul()</methodname></emphasis>: This method multiplies the
  32. given currency with the existing currency object.
  33. </para>
  34. </listitem>
  35. <listitem>
  36. <para>
  37. <emphasis><methodname>mod()</methodname></emphasis>: This method calculates the
  38. remaining value (modulo) from dividing the given currency from the existing
  39. currency object.
  40. </para>
  41. </listitem>
  42. <listitem>
  43. <para>
  44. <emphasis><methodname>compare()</methodname></emphasis>: This method compares
  45. the given currency with the existing currency object. When both values are
  46. equal it returns '0'. When the existing currency value is greater than the
  47. given, this method will return 1. Otherwise you will get '-1' returned.
  48. </para>
  49. </listitem>
  50. <listitem>
  51. <para>
  52. <emphasis><methodname>equals()</methodname></emphasis>: This method compares
  53. the given currency with the existing currency object. When both values are
  54. equal it returns <constant>TRUE</constant>, otherwise
  55. <constant>FALSE</constant>.
  56. </para>
  57. </listitem>
  58. <listitem>
  59. <para>
  60. <emphasis><methodname>isMore()</methodname></emphasis>: This method compares
  61. the given currency with the existing currency object. When the existing
  62. currency is greater than the given one, you will get <constant>TRUE</constant>
  63. in return, otherwise <constant>FALSE</constant>.
  64. </para>
  65. </listitem>
  66. <listitem>
  67. <para>
  68. <emphasis><methodname>isLess()</methodname></emphasis>: This method compares
  69. the given currency with the existing currency object. When the existing
  70. currency is less than the given one, you will get <constant>TRUE</constant>
  71. in return, otherwise <constant>FALSE</constant>.
  72. </para>
  73. </listitem>
  74. </itemizedlist>
  75. <para>
  76. As you can see the multiple methods allow any kind of calculation with
  77. <classname>Zend_Currency</classname>. See the next snippets as example:
  78. </para>
  79. <programlisting language="php"><![CDATA[
  80. $currency = new Zend_Currency(
  81. array(
  82. 'value' => 1000,
  83. 'currency' => 'USD',
  84. )
  85. );
  86. print $currency; // Could return '$ 1.000,00'
  87. $currency->add(500);
  88. print $currency; // Could return '$ 1.500,00'
  89. ]]></programlisting>
  90. <programlisting language="php"><![CDATA[
  91. $currency_2 = new Zend_Currency(
  92. array(
  93. 'value' => 500,
  94. 'currency' => 'USD',
  95. )
  96. );
  97. if ($currency->isMore($currency_2)) {
  98. print "First is more";
  99. }
  100. $currency->div(5);
  101. print $currency; // Could return '$ 200,00'
  102. ]]></programlisting>
  103. </sect1>