[FEATURE] Adjustable decimals for view helper format.currency
authorJosef Florian Glatz <mailto@josefglatz.at>
Wed, 27 Feb 2013 16:54:06 +0000 (17:54 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Tue, 12 Mar 2013 19:02:13 +0000 (20:02 +0100)
Adds an extra attribute to set number of decimal points.

Releases: 6.1
Resolves: #45673
Fluid Package Issue: #45923
Change-Id: I687ced95a0b7a081a669dc8883d39b19542d691c
Reviewed-on: https://review.typo3.org/18528
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
typo3/sysext/fluid/Classes/ViewHelpers/Format/CurrencyViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Format/CurrencyViewHelperTest.php

index 63557d5..27f5337 100644 (file)
@@ -23,7 +23,7 @@ namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
  * </output>
  *
  * <code title="All parameters">
- * <f:format.currency currencySign="$" decimalSeparator="." thousandsSeparator="," prependCurrency="TRUE" separateCurrency="FALSE">54321</f:format.currency>
+ * <f:format.currency currencySign="$" decimalSeparator="." thousandsSeparator="," prependCurrency="TRUE" separateCurrency="FALSE" decimals="2">54321</f:format.currency>
  * </code>
  * <output>
  * $54,321.00
@@ -47,17 +47,18 @@ class CurrencyViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHe
         * @param string $thousandsSeparator (optional) The thousands separator.
         * @param boolean $prependCurrency (optional) Select if the curreny sign should be prepended
         * @param boolean $separateCurrency (optional) Separate the currency sign from the number by a single space, defaults to true due to backwards compatibility
+        * @param int $decimals (optional) Set decimals places.
         * @return string the formatted amount.
         * @api
         */
-       public function render($currencySign = '', $decimalSeparator = ',', $thousandsSeparator = '.', $prependCurrency = FALSE, $separateCurrency = TRUE) {
+       public function render($currencySign = '', $decimalSeparator = ',', $thousandsSeparator = '.', $prependCurrency = FALSE, $separateCurrency = TRUE, $decimals = 2) {
                $floatToFormat = $this->renderChildren();
                if (empty($floatToFormat)) {
                        $floatToFormat = 0.0;
                } else {
                        $floatToFormat = floatval($floatToFormat);
                }
-               $output = number_format($floatToFormat, 2, $decimalSeparator, $thousandsSeparator);
+               $output = number_format($floatToFormat, $decimals, $decimalSeparator, $thousandsSeparator);
                if ($currencySign !== '') {
                        $currencySeparator = $separateCurrency ? ' ' : '';
                        if ($prependCurrency === TRUE) {
index 8140db5..aa2c030 100644 (file)
@@ -132,6 +132,26 @@ class CurrencyViewHelperTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase
                $actualResult = $viewHelper->render();
                $this->assertEquals('12,00', $actualResult);
        }
+
+       /**
+        * @test
+        */
+       public function viewHelperRendersValuesWithoutDecimals() {
+               $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\CurrencyViewHelper', array('renderChildren'));
+               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('54321'));
+               $actualResult = $viewHelper->render('', ',', '.', FALSE, TRUE, 0);
+               $this->assertEquals('54.321', $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function viewHelperRendersThreeDecimals() {
+               $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\CurrencyViewHelper', array('renderChildren'));
+               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('54321'));
+               $actualResult = $viewHelper->render('', ',', '.', FALSE, TRUE, 3);
+               $this->assertEquals('54.321,000', $actualResult);
+       }
 }
 
 ?>
\ No newline at end of file