Zend_Currency-Value.xml 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.currency.value">
  4. <title>How much is my currency?</title>
  5. <para>
  6. When you are working with currencies then you normally want to display an amount of
  7. money. And when you work with different currencies then you have to do this with three
  8. different things. The amount you want to display, the precision you want to use, and
  9. probably the exchange rate.
  10. </para>
  11. <sect2 id="zend.currency.value.money">
  12. <title>Working with currency values</title>
  13. <para>
  14. The currency value, a.k.a. the money, you want to use can easily be set by using the
  15. <property>value</property> option.
  16. </para>
  17. <programlisting language="php"><![CDATA[
  18. $currency = new Zend_Currency(
  19. array(
  20. 'value' => 1000,
  21. 'currency' => 'USD',
  22. )
  23. );
  24. print $currency; // Could return '$ 1.000'
  25. ]]></programlisting>
  26. <para>
  27. Using the <methodname>setFormat()</methodname> method with this array option, and
  28. also by using the <methodname>setValue()</methodname> method you can set the value
  29. afterwards.
  30. </para>
  31. <programlisting language="php"><![CDATA[
  32. $currency = new Zend_Currency(
  33. array(
  34. 'value' => 1000,
  35. 'currency' => 'USD',
  36. )
  37. );
  38. print $currency->setValue(2000); // Could return '$ 2.000'
  39. ]]></programlisting>
  40. <para>
  41. With the <methodname>getValue()</methodname> method you will get the actual set
  42. value.
  43. </para>
  44. </sect2>
  45. <sect2 id="zend.currency.value.precision">
  46. <title>Using precision on currencies</title>
  47. <para>
  48. When working with currencies they you probably also have to handle precision.
  49. Most currencies use a precision of 2. This means that when you have 100 US dollars
  50. you could also have 50 cents. The related value is simply a floating value.
  51. </para>
  52. <programlisting language="php"><![CDATA[
  53. $currency = new Zend_Currency(
  54. array(
  55. 'value' => 1000.50,
  56. 'currency' => 'USD',
  57. )
  58. );
  59. print $currency; // Could return '$ 1.000,50'
  60. ]]></programlisting>
  61. <para>
  62. Of course, as the default precision is 2, you will get '00' for the decimal value
  63. when there is no precision to display.
  64. </para>
  65. <programlisting language="php"><![CDATA[
  66. $currency = new Zend_Currency(
  67. array(
  68. 'value' => 1000,
  69. 'currency' => 'USD',
  70. )
  71. );
  72. print $currency; // Could return '$ 1.000,00'
  73. ]]></programlisting>
  74. <para>
  75. To get rid of this default precision you could simply use the
  76. <property>precision</property> option and set it to '0'. And you can set any other
  77. precision you want to use between 0 and 9. All values will be rounded or streched
  78. when they don't fit the set precision.
  79. </para>
  80. <programlisting language="php"><![CDATA[
  81. $currency = new Zend_Currency(
  82. array(
  83. 'value' => 1000,30,
  84. 'currency' => 'USD',
  85. 'precision' => 0
  86. )
  87. );
  88. print $currency; // Could return '$ 1.000'
  89. ]]></programlisting>
  90. </sect2>
  91. </sect1>