From 6a301f1389b55ad4326fb881e40ccc7c468fb3a9 Mon Sep 17 00:00:00 2001
From: Ralf Zimmermann <ralf.zimmermann@tritum.de>
Date: Tue, 22 Sep 2015 15:16:10 +0200
Subject: [PATCH] [BUGFIX] EXT:form - avoid number reduction in currency filter

Avoid a number reduction each time the filter is applied.
Preserve the decimal places.

Resolves: #69807
Releases: master
Change-Id: I1aed27489ea9a7b7d1f3a3e593803bf075ea25a9
Reviewed-on: https://review.typo3.org/43475
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Bjoern Jacob <bjoern.jacob@tritum.de>
Tested-by: Bjoern Jacob <bjoern.jacob@tritum.de>
Reviewed-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Tested-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
---
 .../Classes/Domain/Filter/CurrencyFilter.php  | 16 +++++++-
 .../Tests/Unit/Filter/CurrencyFilterTest.php  | 38 +++++++++++++++++--
 2 files changed, 49 insertions(+), 5 deletions(-)

diff --git a/typo3/sysext/form/Classes/Domain/Filter/CurrencyFilter.php b/typo3/sysext/form/Classes/Domain/Filter/CurrencyFilter.php
index c55668609409..94d2da8bd577 100644
--- a/typo3/sysext/form/Classes/Domain/Filter/CurrencyFilter.php
+++ b/typo3/sysext/form/Classes/Domain/Filter/CurrencyFilter.php
@@ -89,7 +89,21 @@ class CurrencyFilter extends AbstractFilter implements FilterInterface
      */
     public function filter($value)
     {
-        $value = (double) ((string)$value);
+        $value = str_replace(
+            array(
+                $this->thousandSeparator,
+                $this->decimalsPoint,
+            ),
+            array(
+                '',
+                '.'
+            ),
+            (string)$value
+        );
+
+        // replace all non numeric characters, decimalPoint and negativ sign
+        $value = preg_replace("/[^0-9.-]/", "", $value);
+        $value = (double)$value;
         return number_format($value, 2, $this->decimalsPoint, $this->thousandSeparator);
     }
 }
diff --git a/typo3/sysext/form/Tests/Unit/Filter/CurrencyFilterTest.php b/typo3/sysext/form/Tests/Unit/Filter/CurrencyFilterTest.php
index 2bd6198aab7b..9cf49fc96cec 100644
--- a/typo3/sysext/form/Tests/Unit/Filter/CurrencyFilterTest.php
+++ b/typo3/sysext/form/Tests/Unit/Filter/CurrencyFilterTest.php
@@ -33,29 +33,59 @@ class CurrencyFilterTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     {
         return array(
             '1200 => 1.200,00' => array(
-                1200, // input
+                '1200', // input
                 '.', // thousand separator
                 ',', // decimal point
                 '1.200,00' // expected
             ),
             '0 => 0,00' => array(
-                0,
+                '0',
                 null,
                 ',',
                 '0,00'
             ),
             '3333.33 => 3,333.33' => array(
-                3333.33,
+                '3333.33',
                 ',',
                 '.',
                 '3,333.33'
             ),
             '1099.33 => 1 099,33' => array(
-                1099.33,
+                '1099.33',
                 ' ',
                 ',',
                 '1 099,33'
             ),
+            '1200,00 => 1.200,00' => array(
+                '1200,00', // input
+                '.', // thousand separator
+                ',', // decimal point
+                '1.200,00' // expected
+            ),
+            '1.200,00 => 1.200,00' => array(
+                '1.200,00', // input
+                '.', // thousand separator
+                ',', // decimal point
+                '1.200,00' // expected
+            ),
+            '1.200 => 1.200,00' => array(
+                '1.200', // input
+                '.', // thousand separator
+                ',', // decimal point
+                '1.200,00' // expected
+            ),
+            '-1 => -1,00' => array(
+                '-1', // input
+                '.', // thousand separator
+                ',', // decimal point
+                '-1,00' // expected
+            ),
+            '1.200 => 1.200,00' => array(
+                '1.200', // input
+                '.', // thousand separator
+                ',', // decimal point
+                '1.200,00' // expected
+            ),
         );
     }
 
-- 
GitLab